Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 618
Jozan Avatar asked Aug 20 '10 16:08

Jozan


1 Answers

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];
like image 184
andyvn22 Avatar answered Oct 11 '22 14:10

andyvn22