I have a TextView in which I select some text and apply attributes to the selected text, successfully.
After the update of the NSMutableAttributedString with the desired changes I take my TextView and update its attributed text:
textView.attributedText = NSMutableAttributedStringText // pseudo-example
But this attribution replaces the whole text of the Text View (keeping the previous attributes ofc);
Is there any way of just updating the textView.attributedText
change, instead of replacing the whole text every time I've made a change?
I just did this earlier this week.
Create a mutable copy of attributedText
, update the mutable copy, create an immutable copy of the updated string.
guard let text = textView.attributedText?.mutableCopy() as? NSMutableAttributedString else { return }
text.addAttribute(NSForegroundColorAttributeName, value: color, range: selectedRange)
textView.attributedText = text.copy() as? NSAttributedString