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

时间:2010-08-20 16:09:17

标签: objective-c cocoa 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];

如何改变“myTitle”的颜色f.e.蓝色?像PeerGuardian这样的应用程序在其列表被禁用时会将其状态栏项目标题更改为红色,所以我想这是可能的。

谢谢!

2 个答案:

答案 0 :(得分:5)

使用NSStatusItem的{​​{1}}方法,并为其指定适当颜色的-setAttributedTitle

NSAttributedString

答案 1 :(得分:1)

Swift 4版本:

let attributes = [NSAttributedStringKey.foregroundColor: NSColor.blue]
let attributedText = NSAttributedString(string: "myTitle", attributes: attributes)

let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
statusItem.attributedTitle = attributedText