cocoafan
cocoafan

Reputation: 4894

How to color Text in Popup menu of NSComboBox?

I'm using a NSComboBox and want to mark some of the items in the popup list appear in red. I couldn't find a proper Method to override in NSComboBoxCell. Any idea?

Upvotes: 1

Views: 2110

Answers (2)

Marc Charbonneau
Marc Charbonneau

Reputation: 40517

You'll need to modify the popup button's menu items directly, but it's not very hard. You shouldn't even need to subclass, you can do it all from the 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'll probably want to copy attributes from the existing attributed string title so the font and size remain the same, but that should get you started.

Upvotes: 1

Matt Ball
Matt Ball

Reputation: 6618

How about using NSCell's -setAttributedStringValue: method? Just create an NSAttributedString which has the color you want set for the NSForegroundColorAttributeName key and you should be good to go.

Upvotes: 0

Related Questions