objective-c - UIAppearance 对 Label 文本颜色没有影响

标签 objective-c uitableview ios9 uiappearance

我正在使用 UIAppearance 设置我应用中所有标签的文本颜色。但是文本颜色没有改变。

这是我如何创建标签的示例

//show the loading message
MessageLabel *messageLabel = [[MessageLabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
messageLabel.text = @"\n\nLoading ...\n\n";
messageLabel.numberOfLines = 0;
messageLabel.textAlignment = NSTextAlignmentCenter;
[messageLabel sizeToFit];
self.tableview.backgroundView = messageLabel;

这里是我如何设置文本颜色

[[MessageLabel appearance] setTextColor:[UIColor blackColor]];

需要注意的是所有这些 MessageLabel 都是 UITableView 的 BackgroundViews

最佳答案

如前所述,UILabel 的 textColor 不适用于 UIAppearance。我通过添加一个只包装 textColor 的 UIAppearance 兼容属性来解决这个问题。在 Swift 中,这看起来像

class MessageLabel: UILabel {

    @objc dynamic var configurableTextColor: UIColor {
        get {
            return textColor
        }
        set {
            textColor = newValue
        }
    }

    ...
}

然后配置外观:

MessageLabel.appearance().configurableTextColor = .black

关于objective-c - UIAppearance 对 Label 文本颜色没有影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34466850/

相关文章:

ios - 如果我插入或删除行,UITableView 部分标题会消失

ios - 可变高度 subview Controller 作为 UITableView 的 tableHeaderView

swift - 当我单击 "Replay"按钮时,仅运行 GameScene.sks,而不运行 GameScene.swift...如何让两者同时玩?

iOS 9 - LocationManager 更新位置过于频繁

objective-c - 使用opencv或其他技术识别出现在图像ios四个角上的黑色图案

ios - 加载 UIView 时的事件指示器

ios - 如何在iOS中使用RMStore获取自动续订订阅的通知

ios - 如何在自定义单元格中设置详细附件类型

objective-c - 查找数组中重复次数最多的对象

swift - 如何将值从 navigationController 传递到 TabBarController