Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control the text color of an NSTextField when it is displaying a placeholder marker?

When the NSTextField (Label) is bound to a controller selection with bindings, and I have specified placeholder values for the Multiple Values Marker, No Selection Marker, etc it draws the text with a gray color that does not show up well on a dark background.

Is there a way to change the text color it uses to display the placeholder text?

like image 378
robottobor Avatar asked Nov 16 '09 03:11

robottobor


1 Answers

Use an attributed string specifying the color you want, like this:

NSDictionary *blueDict = [NSDictionary dictionaryWithObject: [NSColor blueColor]
                        forKey: NSForegroundColorAttributeName];
NSAttributedString *blueString = [[[NSAttributedString alloc] initWithString: @"test"
                                attributes: blueDict] autorelease];

Then you can either set the placeholder attributed string directly:

[[field cell] setPlaceholderAttributedString: blueString];

or do it through a binding, for example:

[field2 bind: @"value" toObject: [NSUserDefaults standardUserDefaults]
        withKeyPath: @"foo"
        options: [NSDictionary dictionaryWithObject: blueString forKey: NSNullPlaceholderBindingOption]];
like image 70
Nicholas Riley Avatar answered Sep 23 '22 19:09

Nicholas Riley