Swift NSAttributedString.Key.foregroundColor 在某些较旧的 iOS 版本中不起作用

问题描述

我在向 UILabel 文本添加多种颜色时使用了以下扩展程序。结果在 iOS 13 及更高版本中按预期显示,虽然后来知道它会导致 iOS 12.4 出现问题,因为无论任何更改都显示认的 UILabel 文本颜色

我最初也在 ViewDidLoad() 中使用属性初始化 NSMutableAttributedString。想知道是什么原因导致它在较旧的 iOS 版本中不起作用。

extension NSMutableAttributedString {
    
    func setColorForText(textForAttribute: String,withColor color: UIColor) {
        let range: NSRange = self.mutableString.range(of: textForAttribute,options: .caseInsensitive)
        self.addAttribute(NSAttributedString.Key.foregroundColor,value: color,range: range)
    }
    
    func setColorAndFontForText(textForAttribute: String,withColor color: UIColor,withFont font: UIFont) {
        let range: NSRange = self.mutableString.range(of: textForAttribute,range: range)
        self.addAttribute(NSAttributedString.Key.font,value: font,range: range)
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)