繁体   English   中英

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

[英]How to change color of status bar item title in Objective-C/Cocoa?

//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”的颜色更改为蓝色?

当列表被禁用时,像 PeerGuardian 这样的一些应用程序会将它们的状态栏项目标题更改为红色,所以我想这是可能的。

使用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];

斯威夫特 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM