NSButton - set text color in disabled mode

For some reason, when my button is disabled, the text color turns white. I want it to stay black - how can I do this?

+9
objective-c cocoa macos nsbutton
source share
6 answers

You can set text, image, colors, fonts, etc. for different button status: normal, highlighted, disabled, etc.

This can be done in Interface Builder by changing the state with a drop-down list.

-6
source share

You can subclass NSButtonCell and override the 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]; } 

Thus, when the button is disabled, the text will have the same text color if the button is turned on.

+26
source share

Also check this out

 [btnInfo.cell setImageDimsWhenDisabled:NO]; 
+7
source share

You can override the private method in NSButtonCell:

 - (BOOL)_textDimsWhenDisabled { return NO; } - (BOOL)_shouldDrawTextWithDisabledAppearance { return NO; } 

I filled out the radar for the public method: rdar: // 19218619

+2
source share

Update for quick 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 cause the text attributes to be the same as when the button is turned on.

0
source share

In Mojave, any overriding of drawing methods makes it impossible to change the backgroundColor of the NS button when highlighted. Therefore, I would recommend using

- (BOOL)_shouldDrawTextWithDisabledAppearance

for this. 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 a subclass of NSButtonCell:

 override func _shouldDrawTextWithDisabledAppearance() -> Bool { return false } 
0
source share

All Articles