How to color the text in the NSComboBox popup menu?

I am using NSComboBox and I want to note that some of the items in the popup are displayed in red. I couldn't find the correct method to override in NSComboBoxCell. Any idea?

+1


a source to share


2 answers


You will need to change the menu items of the popup button right away, but it's not very difficult. You don't even need subclasses, you can do it all from a controller.

NSMenu *menu = [popUpButton menu];
NSMenuItem *item = [menu itemWithTag:100];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSColor redColor], NSForegroundColorAttributeName, nil];
NSAttributedString *string = [[NSAttributedString alloc] initWithString:[item title] attributes:attributes];

[item setAttributedTitle:string];

      



You probably want to copy the attributes from the existing title attribute to keep the font and size the same, but that should get you started.

+1


a source


How about using a method NSCell

-setAttributedStringValue:

? Just create NSAttributedString

which has the color you want to set for the key NSForegroundColorAttributeName

and you should be good to go.



0


a source







All Articles