How to change the color of the NSToolbarItem label

I would like to change the color of all labels NSToolbarItem


I need to set it to white because it matches my background color better NSWindow

, but it defaults to black and I haven't found a way to change it either in Interface Builder or directly in code ( NSToolbarItem

implements setLabel

, but it just sets the text string).

If possible, I would like to avoid:

  • Replacing the whole NSToolbar

    with a custom one NSView

    . Would feel like reinventing the wheel for me.
  • You need to create a custom one NSView

    inside NSToolbarItem

    . That would mean leaving all of its labels blank and adding a bright label inside the custom view.

source to share

3 answers

If anyone is interested, I solved it:

  • Using custom views internally NSToolbarItem

    containing both a button and a label.
  • Show icon only instead of icon + label in NSToolbar

    to hide the default label.

I had to deal with another issue related to a bug with Interface Builder: the custom view was not showing at all. I was able to fix it thanks to this answer .



You can change it using NSMutableAttributeString . For example:

-(void) awakeFromNib{
NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:self.label];
NSRange titleRange = NSMakeRange(0, title.length);
[title addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:titleRange];
[self setLabel:title]; }




You must subclass NSBarButtonItem and override the drawRect method to do this. Otherwise, it will use [NSColor controlTextColor]

or [NSColor disabledControlTextColor]




All Articles