3

I'd like to change the color of all of my NSToolbarItem's labels.

I need to set it to white because it suits better to the background color of my NSWindow, but it defaults to black and I haven't found a way to change it neither in Interface Builder nor directly by code (NSToolbarItem implements setLabel, but it just sets the text string).

If possible, I'd like to avoid:

  • Replacing the whole NSToolbar by a custom NSView. Would feel like reinventing the wheel to me.
  • Having to create custom NSViews inside NSToolbarItem. It would imply having to leave blank all of its labels and adding the white-colored label inside the custom view.
Aravindhan
  • 15,608
  • 10
  • 56
  • 71
msoler
  • 2,930
  • 2
  • 18
  • 30

3 Answers3

1

In case anyone is interested, I solved it by:

  • Using custom views inside NSToolbarItems containing both a button and a label.
  • Displaying icon only instead of icon + label in NSToolbar in order to hide the default label.

I had to deal with another problem 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.

Community
  • 1
  • 1
msoler
  • 2,930
  • 2
  • 18
  • 30
0

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]; }
Ray
  • 51
  • 5
-2

You have to subclass NSBarButtonItem and override the drawRect method for that. Otherwise it will use [NSColor controlTextColor] or [NSColor disabledControlTextColor].

Boris
  • 360
  • 2
  • 12
  • Started trying what you suggested and NSBarButtonItem class doesn't exist in AppKit. Also, I was asking for NSToolbarItem, which inherits from NSObject so there's no drawRect method. – msoler Apr 20 '12 at 15:09