How to set color of NSPopupButton Menu Item

后端 未结 2 1829
青春惊慌失措
青春惊慌失措 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];
    }
    

提交回复
热议问题