重置文本时外观代理被覆盖

Appearance Proxy overridden when resetting text

我正在使用 UI外观代理设置我的 UI。我将我的 UI 文本字段设置为具有如下自定义字体:

[[UILabel appearanceWhenContainedIn:[RDTextField class], nil] setTextColor:[UIColor rds_purple]];
[[UILabel appearanceWhenContainedIn:[RDTextField class], nil] setFont:[UIFont rds_ralewayWithSize:20.0]];

这非常有效,当我的文本字段出现在屏幕上时,它们的字体是正确的。但是,我允许我的用户像这样随机播放文本字段中的字符串:

- (IBAction)shuffleValuesButtonPressed:(id)sender {
    self.randomStringTextfield.text = [self randomString];
}

发生这种情况时,字体会从我的自定义字体变成默认的黑色字体。为什么会发生这种情况,让我的新字符串成为我在外观代理中设置的自定义字体的解决方案是什么?

根据 Apple 开发者文档:

iOS applies appearance changes when a view enters a window, it doesn’t change the appearance of a view that’s already in a window. To change the appearance of a view that’s currently in a window, remove the view from the view hierarchy and then put it back.

所以这个解决方案的工作原理是删除所有内容并将其添加回去,但可能有更好的方法:

- (IBAction)shuffleValuesButtonPressed:(id)sender {
    self.randomStringTextfield.text = [self randomString];
    [self refreshViews];
}

- (void)refreshViews {
    for (UIWindow *window in [UIApplication sharedApplication].windows) {
        for (UIView *view in window.subviews) {
            [view removeFromSuperview];
            [window addSubview:view];
        }
    }
}