Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 如何设置NSPoupButton菜单项的颜色_Cocoa - Fatal编程技术网

Cocoa 如何设置NSPoupButton菜单项的颜色

Cocoa 如何设置NSPoupButton菜单项的颜色,cocoa,Cocoa,这是一个答案,而不是一个问题。在网上搜索时,我只找到了这个问题()的一个经过黑客攻击、扭曲的答案,可以这样更优雅地回答: NSArray *itemArray = [scalePopup itemArray]; int i; NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSColor r

这是一个答案,而不是一个问题。在网上搜索时,我只找到了这个问题()的一个经过黑客攻击、扭曲的答案,可以这样更优雅地回答:

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];
}
NSArray*itemArray=[scalePopup itemArray];
int i;
NSDictionary*属性=[NSDictionary]
具有ObjectsSandKeys的字典:
[NSColor redColor],NSForegroundColorAttributeName,
[NSFont systemFontOfSize:[NSFont systemFontSize]],
NSFontAttributeName,无];
对于(i=0;i<[itemArray count];i++){
NSMenuItem*item=[itemArray objectAtIndex:i];
NSAttributedString*as=[[NSAttributedString alloc]
initWithString:[项目标题]
属性:属性];
[项目集属性标题:as];
}

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

我也遇到了同样的问题

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

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

请注意,
setAttributeTitle
是在10.3中引入的,它仅在2003年才可用,比您提到的电子邮件交换时间晚。所以那是他们唯一能做的事。太棒了!稍微修改一下,它也可以很好地为每个菜单项添加一个简单的图像。这种方法的一个问题是,你似乎无法用这种方法将突出显示的状态设置为“反向”文本。我一直在试图找到一种方法来实现这一点,而不必在
NSMenu
中查看自定义视图。到目前为止没有运气。这不是这个网站的工作方式。如果你想回答你自己的问题,你应该假装你处于危险之中,并用问题的形式表达出来。有关详细信息,请参见。奇怪的是,字体大小仍然太小。为了更好地匹配常规商品,我必须使用
[NSFont-menuFontOfSize:14.0]