【问题标题】:How to set color of NSPopupButton Menu Item如何设置 NSPopupButton 菜单项的颜色
【发布时间】:2022-04-05 14:04:54
【问题描述】:

这是一个答案,而不是一个问题。在网上搜索,我只找到了这个问题(http://www.cocoabuilder.com/archive/cocoa/58379-changing-the-text-color-of-an-nsmenuitem-in-an-nspopupbutton.html)的一个真正被破解、扭曲的答案,可以像这样更优雅地回答:

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];
}

【问题讨论】:

  • 请注意,setAttributedTitle 是在 10.3 中引入的,它仅在 2003 年可用,比您提到的电子邮件交换。所以这是他们唯一能做的。
  • 太棒了!稍作修改,它也适用于为每个菜单项添加简单的图像。
  • 这种方法的一个问题是,您似乎无法使用这种方法使用“反向”文本设置突出显示状态的样式。我一直试图找出一种方法来做到这一点,而无需在NSMenu 中使用自定义视图。到目前为止没有运气。

标签: cocoa


【解决方案1】:

请注意,上面的问题实际上就是答案。网络上有很多基于旧 API 的过于复杂的解决方案的链接,我认为写这篇文章作为参考会很有帮助。

【讨论】:

  • 这不是本网站的运作方式。如果你想回答你自己的问题,你应该假装你在危险中并以问题的形式表达出来。详情请参阅FAQ
  • 奇怪的是,字体还是太小了。为了更好地匹配常规物品,我不得不使用[NSFont menuFontOfSize:14.0]
【解决方案2】:

我也遇到了同样的问题。

要保留原始文本属性, 我的解决方案在这里:

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];
}

【讨论】:

    【解决方案3】:

    Swift 5.5

    为菜单项分配一个属性字符串。例如,如果之前选择了该项目,我通过分配一个粗体的绿色属性字符串来更改标题颜色。

    我为之前选择的标题设置了一个属性,因此如果选择了另一个标题,我可以轻松删除属性字符串。

    var previousTitle: String = ""
    

    然后在我的方法中,我执行以下操作

    if !previousTitle.isEmpty {
        // Remove the previously assigned attributed title
        myPopUpButton.selectItem(withTitle: previousTitle)
        myPopUpButton.selectedItem?.attributedTitle = nil
    }
    previousTitle = <some previously selected title>
    myPopUpButton.selectItem(withTitle: previousTitle)
    let font = NSFont.systemFont(ofSize: 13).bold
    let foreground = NSColor.green
    let attrString = NSAttributedString(string: previousTitle, attributes: [NSAttributedString.Key.font: font, NSAttributedString.Key.foregroundColor: foreground])
    myPopUpButton.selectedItem?.attributedTitle = attrString
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多