微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。