I have a label with the string shown below. I would like secondVariable
to be a different color. I think I understand how to change the color. My problem is getting the range of secondVariable
.
let str = "\(firstVariable) some random text \(secondVariable)"
let secondVariableRange = str.range(???)
let secondVariableNSRange = NSRange(secondVariableRange, in: str)
let attributedString = NSMutableAttributedString.init(string:
"\(firstVariable) some random text \(secondVariable)")
attributedString.addAttribute(.foregroundColor, value: UIColor.white,
range: NSRange(secondVariableNSRange, in: attributedString)