How to set color of NSPopupButton Menu Item

后端 未结 2 1808
青春惊慌失措
青春惊慌失措 2021-02-04 04:55

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-cha

相关标签:
2条回答
  • 2021-02-04 05:08

    I just got a same problem.

    To preserve original text attributes, my solution is here:

    NSRange range = NSMakeRange(0, 0);
    NSAttributedString *cellStr = [[self.popup cell] attributedTitle];
    NSMutableDictionary *cellAttr = [[cellStr attributesAtIndex:range.location effectiveRange:&range] mutableCopy];
    [cellAttr setObject:[NSColor redColor] forKey:NSForegroundColorAttributeName];
    
    NSArray *menuItems = [self.popup itemArray];    
    for (NSMenuItem *menu in menuItems ) {
        NSString *orgTitle = [menu title];
        NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:orgTitle attributes:cellAttr];
    
        [menuItem setAttributedTitle:title];
    }
    
    0 讨论(0)
  • 2021-02-04 05:11

    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.

    0 讨论(0)
提交回复
热议问题