Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set color of NSPopupButton Menu Item

Tags:

cocoa

This is an answer, rather than a question. Searching online, I only found a really hacked, contorted answer to this question (http://www.cocoabuilder.com/archive/cocoa/58379-changing-the-text-color-of-an-nsmenuitem-in-an-nspopupbutton.html), which can be answered more elegantly like so:

NSArray *itemArray = [scalePopup itemArray];
int i;
NSDictionary *attributes = [NSDictionary
                            dictionaryWithObjectsAndKeys:
                            [NSColor redColor], NSForegroundColorAttributeName,
                            [NSFont systemFontOfSize: [NSFont systemFontSize]],
                            NSFontAttributeName, nil];

for (i = 0; i < [itemArray count]; i++) {
    NSMenuItem *item = [itemArray objectAtIndex:i];

    NSAttributedString *as = [[NSAttributedString alloc] 
             initWithString:[item title]
             attributes:attributes];

    [item setAttributedTitle:as];
}
like image 429
snibbe Avatar asked Jan 19 '11 05:01

snibbe


1 Answers

Just a note here that the question above is in fact the answer. There are a lot of links on the web with too complex solutions based on older APIs and I thought it would be helpful to write this posting as a reference.

like image 111
snibbe Avatar answered Oct 15 '22 21:10

snibbe