开发者

Set the background color of selected text in NSTextView?

I am tryin开发者_JAVA百科g to change the background color of a portion of text that is selected by the user to a different color (to add a highlight) to the text. Now I have tried the:

  • (void)setSelectedTextAttributes:(NSDictionary *)attributes

method but that only changes the background color of the selection. I need the to change the background color of text so that it stays highlighted.


As you've discovered, the selected text attributes only apply to the text while it's selected. If you want to add some attributes to the selected text that persist, you'll need to apply those attributes to the underlying NSTextStorage object (which is just a subclass of NSMutableAttributedString).

The way to do that is to use the text view's textStorage method and then apply the attributes using the addAttributes:range: method (or setAttributes:range: depending on whether you want to add to the existing text attributes or replace them altogether).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜