0

I'm using the below extension while adding multiple colors to UILabel text. Results are showing as expected in iOS 13 and above, While came to know that it causes issues in iOS 12.4 as showing default UILabel text color regardless of any changes.

I'm initializing NSMutableAttributedString in ViewDidLoad() initially with attributes as well. Wondering what causes it not to work in older iOS versions.

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, options: .caseInsensitive)
        self.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
        self.addAttribute(NSAttributedString.Key.font, value: font, range: range)
    }
}
  • What's stopping you from running a search and learning how to make an NSAttributedString in an older Swift version? – El Tomato Jul 08 '21 at 23:58
  • I have searched firstly gone through some stack overflow solutions there after asked for the same, Those answers didn't worked for me, So need clarification, Do I need to consider it as OS or Device specific issue or there's still a solution? That's the only reason I asked to that get more accurate solution. – Hardik Buddh Jul 09 '21 at 04:29
  • @ElTomato Issue is not about an older Swift version, it's about not having the same behavior between iOS different versions, no? Other changes works? For instance, you use it twice, but the font change works? – Larme Jul 09 '21 at 07:32
  • Yes font changes works, But the foreground color doesn't. – Hardik Buddh Jul 09 '21 at 07:54

0 Answers0