I try to underline part of a string, for example, a \'string\' part in \'test string\' string. I\'m using NSMutableAttributedString and my sol
I used the following extension (using exidy's function) in playground/simulator and it worked fine , you may change/add attributes depending on your needs
extension NSMutableAttributedString
{
func changeWordsColour(terms:[NSString])
{
let string = self.string as NSString
self.addAttribute(NSForegroundColorAttributeName, value: UIColor.brownColor(), range: NSMakeRange(0, self.length))
for term in terms
{
let underlineRange = string.rangeOfString(term as String)
self.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: underlineRange)
}
}
}
let myStr = NSMutableAttributedString(string: "Change Words Colour")
myStr.changeWordsColour(["change","Colour"])