How to change color of status bar item title in Objective-C/Cocoa?
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];
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