This is a follow up to this question How can I change style of some words in my UITextView one by one in Swift?
Thanks to @Josh's help I was able to write a piece of code that highlights each word that begins with #
- and do it one by one. My final code for that was:
func highlight (to index: Int) {
let regex = try? NSRegularExpression(pattern: "#(\\w+)", options: [])
let matches = regex!.matches(in: hashtagExplanationTextView.text, options: [], range: NSMakeRange(0, (hashtagExplanationTextView.text.characters.count)))
let titleDict: NSDictionary = [NSForegroundColorAttributeName: orangeColor]
let titleDict2: NSDictionary = [NSForegroundColorAttributeName: UIColor.red]
let storedAttributedString = NSMutableAttributedString(string: hashtagExplanationTextView.text!, attributes: titleDict as! [String : AnyObject])
let attributedString = NSMutableAttributedString(attributedString: storedAttributedString)
guard index < matches.count else {
return
}
for i in 0..<index{
let matchRange = matches[i].rangeAt(0)
attributedString.addAttributes(titleDict2 as! [String : AnyObject], range: matchRange)
}
hashtagExplanationTextView.attributedText = attributedString
if #available(iOS 10.0, *) {
let _ = Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { _ in
self.highlight(to: index + 1)
}
} else {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.highlight(to: index + 1)
}
}
}
This works fine, but I would like to change the logic so that it does not highlight the #
words, but highlights (one by one) words from preselected array of those words.
So I have this array var myArray:[String] = ["those","words","are","highlighted"]
and how can I put it instead of regex match in my code?
The proposed solutions so far suggest that you go through each word and then search them in the text view. This works, but you are traversing the text way too many times.
What I would suggest is to enumerate all the words in the text and see if they match any of the words to highlight:
This should be fine for small texts. For long texts, going through everything on each change can really hurt performance. In that case, I'd recommend using a custom
NSTextStorage
subclass. There you would have better control over what range of text have changed and apply the highlight only to that section.For this new requirement you don't need a regex, you can just iterate over your array of words and use
rangeOfString
to find out if that string exists and set the attributes for the located range.To match the original functionality, after you find a matching range you need to search again, starting from the end of that range, to see if there is another match later in your source text.
I believe you are using regex to get an array of
NSRange
. Here, you need a slightly different datastructure like[String : [NSRange]]
. Then you can userangeOfString
function to detect theNSRange
where the word is located. You can follow the example given below for that:Now that you have an array of
NSRange
i.e, [NSRange] for each word stored in a dictionary, you can highlight each word accordingly in yourUITextView
.Feel free to comment if you have any doubts regarding the implementation :)