如何设置NSPopupButton菜单项的颜色 [英] How to set color of NSPopupButton Menu Item

查看:77
本文介绍了如何设置NSPopupButton菜单项的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个答案,而不是一个问题.在网上搜索时,我只找到了这个问题的真正被歪曲的答案(

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

推荐答案

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

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.

这篇关于如何设置NSPopupButton菜单项的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆