Jozan
Jozan

Reputation: 84

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

How to change color of "myTitle" to blue?

Some applications like PeerGuardian change their status bar item titles to red when their lists are disabled, so I guess this is somehow possible.

Upvotes: 6

Views: 2345

Answers (2)

Alex
Alex

Reputation: 1684

Swift 4 version:

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

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

Upvotes: 2

andyvn22
andyvn22

Reputation: 14824

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

Upvotes: 5

Related Questions