How to paint background of specific range of text in NSTextView
- by Rui Pacheco
Hi,
This is not a specific bug, its more about not knowing how to do something.
I've an NSTextView and I need to paint the background of specific ranges of text when the user clicks on a specific part of the text view. I've tried this but I just get erratic behaviour in the sense that sometimes text loses the foreground color or the background doesn't span the whole range:
NSLayoutManager *layoutManager = [myTextView layoutManager];
[layoutManager removeTemporaryAttribute:NSBackgroundColorAttributeName
forCharacterRange:range];
[[myTextView layoutManager] setTemporaryAttributes:attributes forCharacterRange:range];
For the sake of simplicity assume that range is always a valid string (it is in my testing environment).