TextView.typingAttributes doesn't work for prediction

I have a text view that is filled with the default text in blue, now my goal is no matter what editing I do in the text, it should be red. I use texView.typingAttributes

to achieve this and it works almost well, except when I use prediction or auto-correct starting with any words that are in blue, in which case the predicted or auto-corrected word is still blue. If I use the forecast outside the blue area it works great. Is there something else I should do to make a prediction? Below is my current code that I am using to achieve this.

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {

    textView.typingAttributes[NSForegroundColorAttributeName] = UIColor.red

    return true
}

      

+3


source to share


1 answer


For Correction, you need to apply the color attribute property to the selected row range. Now it works, added code like below:



func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {

    textView.typingAttributes[NSForegroundColorAttributeName] = UIColor.red

    let colorAttribute = [NSForegroundColorAttributeName: UIColor.red]
    textView.textStorage.addAttributes(colorAttribute, range: range)

        return true
    }

      

0


source







All Articles