我正在使用NSComboBox,并希望将弹出列表中的某些项目标记为红色。
我在NSComboBoxCell中找不到要重写的正确方法。任何的想法?

最佳答案

您需要直接修改弹出按钮的菜单项,但这并不难。您甚至不需要子类化,就可以从控制器完成所有操作。

NSMenu *menu = [popUpButton menu];
NSMenuItem *item = [menu itemWithTag:100];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSColor redColor], NSForegroundColorAttributeName, nil];
NSAttributedString *string = [[NSAttributedString alloc] initWithString:[item title] attributes:attributes];

[item setAttributedTitle:string];


您可能需要从现有的属性字符串标题中复制属性,以使字体和大小保持不变,但这应该可以帮助您入门。

关于cocoa - 如何在NSComboBox的弹出菜单中为文本着色?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/901164/

10-16 23:43