如何在Objective-C /可可中更改状态栏项目标题的颜色?
问题描述:
//Create the NSStatusBar and set its length
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
[statusItem setHighlightMode:YES];
[statusItem setTitle:@"myTitle"];
[statusItem setToolTip:@"myToolTip"];
[statusItem setMenu:statusMenu];
[statusItem setEnabled:YES];
如何更改 myTitle的颜色变成蓝色?
How to change color of "myTitle" to blue?
像PeerGuardian这样的某些应用程序在禁用列表时将其状态栏项目标题更改为红色,所以我想这是有可能的。
Some applications like PeerGuardian change their status bar item titles to red when their lists are disabled, so I guess this is somehow possible.
答
使用 NSStatusItem
的 -setAttributedTitle
方法,然后给它一个适当颜色的 NSAttributedString
:
Use NSStatusItem
's -setAttributedTitle
method, and give it an NSAttributedString
of the appropriate color:
NSDictionary *titleAttributes = [NSDictionary dictionaryWithObject:[NSColor blueColor] forKey:NSForegroundColorAttributeName];
NSAttributedString* blueTitle = [[NSAttributedString alloc] initWithString:@"myTitle" attributes:titleAttributes];
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
[statusItem setAttributedTitle:blueTitle];
[blueTitle release];