How can I change the style of preselected words in my text element?

This is a continuation of this question. How do I style some words in my UITextView one at a time in Swift?

Thanks to @Josh's help, I was able to write a piece of code that highlights every word starting with #, and do it one by one. My last code for this 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 words #, but select (one by one) words from a pre-selected array of these words.

So, I have this array var myArray:[String] = ["those","words","are","highlighted"]and how can I put it instead of matching the regular expression in my code?

+4
3

, regex NSRange. , [String : [NSRange]]. rangeOfString NSRange, . :

let wordMatchArray:[String] = ["those", "words", "are", "highlighted"]
let labelText:NSString = NSString(string: "those words, those ldsnvldnvsdnds, are, highlighted,words are highlighted")
let textLength:Int = labelText.length

var dictionaryForEachWord:[String : [NSRange]] = [:]

for eachWord:String in wordMatchArray {

   var prevRange:NSRange = NSMakeRange(0, 0)
   var rangeArray:[NSRange] = []

   while ((prevRange.location + prevRange.length) < textLength) {

      let start:Int = (prevRange.location + prevRange.length)
      let rangeEach:NSRange = labelText.range(of: eachWord, options: NSString.CompareOptions.literal, range: NSMakeRange(start, textLength-start))
      if rangeEach.length == 0 {
         break
      }
      rangeArray.append(rangeEach)
      prevRange = rangeEach
   }

   dictionaryForEachWord[eachWord] = rangeArray
}

, NSRange i.e, [NSRange] , , UITextView.

, :)

+3

, rangeOfString, , .

, , , , , , .

0

, , . , .

- , :

class ViewController: UIViewController {

    @IBOutlet var textView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()
        textView.delegate = self
        highlight()
    }

    func highlight() {
        guard let attributedText = textView.attributedText else {
            return
        }
        let wordsToHighlight = ["those", "words", "are", "highlighted"]
        let text = NSMutableAttributedString(attributedString: attributedText)
        let textRange = NSRange(location: 0, length: text.length)
        text.removeAttribute(NSForegroundColorAttributeName, range: textRange)

        (text.string as NSString).enumerateSubstrings(in: textRange, options: [.byWords]) { [weak textView] (word, range, _, _) in
            guard let word = word else { return }
            if wordsToHighlight.contains(word) {
                textView?.textStorage.setAttributes([NSForegroundColorAttributeName: UIColor.red], range: range)
            } else {
                textView?.textStorage.removeAttribute(NSForegroundColorAttributeName, range: range)
            }
        }
        textView.typingAttributes.removeValue(forKey: NSForegroundColorAttributeName)
    }
}

extension ViewController: UITextViewDelegate {
    func textViewDidChange(_ textView: UITextView) {
        highlight()
    }
}

This should be good for small texts. For long texts, everything through every change can really hurt the work. In this case, I would recommend using a custom subclass NSTextStorage. There you better control what range of text has changed, and apply highlighting only to this section.

0
source

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


All Articles