How can I change the style of some words in my UITextView one by one in Swift?

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!]
    // This will give me an attributedString with the desired 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
}
}

, ​​ .

+1
1

. . . 1 , , .

    func highlight (to index: Int = 0) {
        guard index < matches.count else {
            return
        }
        let titleDict: NSDictionary = [NSForegroundColorAttributeName: orangeColor]
        let attributedString = NSMutableAttributedString(attributedString: storedAttributedString)
        for i in 0..< index {
            let matchRange = matches[i].rangeAt(0)
            attributedString.addAttributes(titleDict as! [String : AnyObject], range: matchRange)
        }
        self.attributedText = attributedString
        let _ = Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { _ in
            self.highlight(to: index + 1)
        }
    }
+1

Source: https://habr.com/ru/post/1660418/


All Articles