I have UITextViewwith many different words next to each other. When a user enters this screen, I want to start highlighting some words, for example:
the first thing he sees is a wall of text:
one two three #four five six #seven eight nine ten
eleven #twelve thirteen fourteen fifteen sixteen
some other words #example test whatever #some thing
then after one second the word fourwill change the style (color), so he will see:
one two three #four five six #seven eight nine ten
eleven #twelve thirteen fourteen fifteen sixteen
some other words #example test whatever #some thing
then after one second another word will be highlighted (and a color one has already been added four):
one two three #four five six #seven eight nine ten
eleven #twelve thirteen fourteen fifteen sixteen
some other words #example test whatever #some thing
etc. Therefore, after a couple of seconds, the user will see:
one two three #four five six #seven eight nine ten
eleven #twelve thirteen fourteen fifteen sixteen
some other words #example test whatever #some thing
and then the text should remain so. How can I achieve this?
I thought about sorting through the words and checking if they are equal to the predefined words, but I have no idea how to bite it - can you help me with this?
===== EDIT
, , #.
, , # textView:
extension UITextView {
func formatTextInTextView() {
self.isScrollEnabled = false
let selectedRange = self.selectedRange
let text = self.text
let font = UIFont(name: "AppleSDGothicNeo-Light", size: 16.0)
let titleDict: NSDictionary = [NSFontAttributeName: font!]
let attributedString = NSMutableAttributedString(string: text!, attributes: titleDict as! [String : AnyObject])
let regex = try? NSRegularExpression(pattern: "#(\\w+)", options: [])
let matches = regex!.matches(in: text!, options: [], range: NSMakeRange(0, (text?.characters.count)!))
for match in matches {
let matchRange = match.rangeAt(0)
let titleDict: NSDictionary = [NSForegroundColorAttributeName: orangeColor]
attributedString.addAttributes(titleDict as! [String : AnyObject], range: matchRange)
}
self.attributedText = attributedString
self.selectedRange = selectedRange
self.isScrollEnabled = true
}
}
, .