如何在objective-C/Cocoa中更改状态栏项目标题的颜色?

Joz*_*zan 5 cocoa objective-c title statusbar

//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];
Run Code Online (Sandbox Code Playgroud)

如何将"myTitle"fe的颜色改为蓝色?像PeerGuardian这样的应用程序在其列表被禁用时将其状态栏项目标题更改为红色,所以我想这在某种程度上是可能的.

谢谢!

and*_*n22 5

使用NSStatusItem-setAttributedTitle方法,并给它一个NSAttributedString合适的颜色:

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];
Run Code Online (Sandbox Code Playgroud)