I'm trying to find every occurrence of a string in UITextView and attribute that occurrence but i attributes the entire textView after the first occurrence of that string. Is it possible to find the location of each occurrence of "hello" and attribute them without attributing the entire textView
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if textView == self.textView {
let nsString = textView.text as NSString
let end = textView.text.characters.count
let range = NSMakeRange(0, end) as NSRange
let hString = nsString.rangeOfString("hello")
let text = NSMutableAttributedString(string: textView.text)
let cTv = textView.attributedText.mutableCopy() as! NSMutableAttributedString
if hString.length > 0{
text.addAttribute(NSForegroundColorAttributeName, value: UIColor.magentaColor(), range: range)
cTv.replaceCharactersInRange(range, withAttributedString: text)
}
textView.attributedText = text
}
return true
}