首页 > 解决方案 > 如何通过界面生成器在属性字符串中使用动态颜色?

问题描述

根据在 iOS 上实现暗模式,我们需要将 foregroundColor 属性设置为新的标签颜色。这是如何使用界面生成器完成的?

我尝试使用“文本颜色”选项并将颜色设置为 Developer->labelColor,但这不起作用。

编辑:由于目前这是不可能的,我使用了这个解决方法:

override func viewDidLoad() {
    // Support Dark Mode
    if #available(iOS 13.0, *) {
        let attributes = NSMutableAttributedString(attributedString: textView.attributedText!)
        attributes.addAttribute(.foregroundColor, value: UIColor.label, range: NSRange(location: 0, length: attributes.mutableString.length))
        textView.attributedText = attributes
    }
}

标签: iosinterface-buildernsattributedstringios-darkmode

解决方案


您不能在 Interface Builder 中执行此操作。您必须.foregroundColor在代码中设置属性。

let mas = NSMutableAttributedString(string:s, attributes:[
    .font:UIFont(name:"GillSans", size:15)!,
    .foregroundColor:UIColor.label,
]

推荐阅读