How to set the color of all text in an NSTextView (and not just text subsequently printed)

I want to change the color of ALL text in an NSTextView. I currently have the code:

NSMutableDictionary* fontAttributes = [[NSMutableDictionary alloc] init]; [fontAttributes setObject: newColour forKey:NSForegroundColorAttributeName]; [self setTypingAttributes:fontAttributes]; 

... but this only changes the color of the text entered after setting the attributes.

Is there an easy way to change the color of all text in a view, and not just what is inserted into insertionPoint?

+4
source share
2 answers
 [textView setTextColor:newColor]; 

You may not have noticed this method because it is actually part of the NSText from which the NSTextView is inherited.

+11
source

You need to set the NSForegroundColorAttributeName attribute of the text view NSTextStorage :

 NSTextStorage* textViewContent = [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]; 
+2
source

Source: https://habr.com/ru/post/1381455/


All Articles