开发者

Changing text color of NSTextView in Interface Builder won't work

开发者 https://www.devze.com 2023-01-15 07:57 出处:网络
I can set background color for NSTextView, also insertion color, but when I try to change text color it just doesn\'t work.

I can set background color for NSTextView, also insertion color, but when I try to change text color it just doesn't work.

开发者_如何转开发I can set the color programmatically before each insert of text, but I'm probably doing something wrong, since Interface Builder offers this options.

Here's what my inspector looks like:

Changing text color of NSTextView in Interface Builder won't work


Here is what I have tried below and the results I've found which led me to my conclusion.

  1. (a)Add NSTextView (b)change color. Result: Does not work

    Changing text color of NSTextView in Interface Builder won't work

  2. (a)Add NSTextView (b)then change the text (c) press enter because it failed when i didnt press enter (d) then change the color. Result: Works

    Changing text color of NSTextView in Interface Builder won't work

If the text property of the NSTextView is not set in Interface-Builder, it does not seem to save the text color property. Thus causing the app to select the default color upon launch. The other properties such as Background-Color and Insertion color however, are saved. This leads me to think this is a bug in interface builder.

As a workaround, you can leave single blank space in the text box and the set your color in interface builder, but this might not be favorable for you depending on what you wish to do with the NSTextView


As far as I can see - there's a bug in appkit as of (10.10) when the text is not initialized. This hack got me out of trouble.

NSTextView *tf = 
tf.delegate = self;

- (void)textDidChange:(NSNotification *)aNotification {
    NSLog(@"notificaiton:%@",aNotification);
    NSMTextView *tv = (NSMTextView *)[aNotification object];
    [tv setTextColor:[NSColor whiteColor]];
    [[tv textStorage] setFont:[NSFont systemFontOfSize:30]];
}


NSTextView gets it's font and font colour from the system wide Font Panel which is a global component - so set the font colour in NotePad and you will most likely find that the font colour is also applied to the NSTextView next time you use it.

You need to override this behaviour to prevent NSTextView from using the system font panel. This seems to be more difficult with Dark Mode now since you should be trying to adopt the default Dark/Light mode font colours and control background colours.

0

精彩评论

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