For some reason, when my button is disabled, the text color turns white. I want it to stay black - how can i do that?
-
you probably already tried button.textcolor =[UIColor blackColor]; but just in case – Radu Jun 16 '11 at 12:01
6 Answers
You can subclass NSButtonCell and override a method:
- (NSRect)drawTitle:(NSAttributedString *)title withFrame:(NSRect)frame inView:(NSView *)controlView
{
if (![self isEnabled]) {
return [super drawTitle:[self attributedTitle] withFrame:frame inView:controlView];
}
return [super drawTitle:title withFrame:frame inView:controlView];
}
In this way, when button is disabled, the text will have the same color of text when button is enabled.

- 366
- 3
- 8
Also check out this
[btnInfo.cell setImageDimsWhenDisabled:NO];

- 3,535
- 3
- 26
- 38

- 1,821
- 4
- 26
- 32
You can override a private method in NSButtonCell:
- (BOOL)_textDimsWhenDisabled {
return NO;
}
- (BOOL)_shouldDrawTextWithDisabledAppearance {
return NO;
}
I filled a radar for a public method: rdar://19218619

- 952
- 4
- 18
Update for swift 4:
override func drawTitle(_ title: NSAttributedString, withFrame frame: NSRect, in controlView: NSView) -> NSRect {
if !self.isEnabled {
return super.drawTitle(self.attributedTitle, withFrame: frame, in: controlView)
}
return super.drawTitle(title, withFrame: frame, in: controlView)
}
This will make text attributes the same as when button is enabled.

- 179
- 1
- 12
-
Ladies and gentleman - we have a winner! Only solution from all that works! – Starwave Feb 09 '21 at 20:30
In Mojave, any override of draw methods makes it impossible to change the backgroundColor of the NSbutton when highlighted. So I would rather recommend to use
- (BOOL)_shouldDrawTextWithDisabledAppearance
for this purpose. If you are using Swift 4, I would do the following in the Bridging header:
#import <AppKit/AppKit.h>
@interface NSButtonCell (Private)
- (BOOL)_shouldDrawTextWithDisabledAppearance;
@end
And in the subclass of NSButtonCell:
override func _shouldDrawTextWithDisabledAppearance() -> Bool {
return false
}

- 1,417
- 18
- 22
You can set text, image, colors, fonts, etc. for different status of a button: normal, highlighted, disabled, etc.
You can do that in Interface Builder by changing the state with the dropdown list.

- 2,027
- 15
- 13
-
-
5
-
@ErikSapir - Since you accepted this answer, I assume you found where in IB this is. Can you share with the rest of us? – pepsi May 29 '12 at 20:21
-
1You're right, sorry. I mixed up UIButton and NSButton... can Erik Sapir remove the checkmark? – Christian Beer Mar 11 '14 at 06:08