views:

247

answers:

2

Hi,

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?

A: 

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.

Matt Ball
This would change the color of the current displayed text. I want to set a different color then black to strings in the popup menu list that appears when you click the triangles. I want to highlight some values in the list. In my use case I want to highlight recommended values the user should select from the popup list.
cocoafan
+1  A: 

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.

Marc Charbonneau
Thank you for the answer. Unfortunately an NSComboBox does not have a menu. NSPopUpButton has a menu. Actually I'm using only the NSComboBoxCell in a NSTableView.
cocoafan