I want to change the color of ALL the text in a NSTextView. Currently, I have code doing this:
NSMutableDictionary* fontAttributes = [[NSMutableDictionary alloc] init];
[fontAttributes setObject: newColour forKey:NSForegroundColorAttributeName];
[self setTypingAttributes:fontAttributes];
... but that only changes the color of text typed after the attributes are set.
Is there an easy way to change the color of all text in the view, not just what is entered at the insertionPoint ?
[textView setTextColor:newColor];
You may not have noticed that method because it is actually part of NSText, from which NSTextView inherits.
You need to set the NSForegroundColorAttributeName
attribute of the text view's NSTextStorage
object:
NSTextStorage* textStorage = [textView textStorage];
//get the range of the entire run of text
NSRange area = NSMakeRange(0, [textStorage length]);
//remove existing coloring
[textStorage removeAttribute:NSForegroundColorAttributeName range:area];
//add new coloring
[textStorage addAttribute:NSForegroundColorAttributeName
value:[NSColor yellowColor]
range:area];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With