6

When I set my buttons to disabled, the text becomes gray (was black before). In my window, the result is that the text is not readable when button is disabled.

I looked all over in the documentation of NSButton/NSButtonCell/NSCell/NSControl, but I did not find any way to keep the text black. Do you know how I can do that?

csano
  • 13,266
  • 2
  • 28
  • 45
Erik Sapir
  • 23,209
  • 28
  • 81
  • 141
  • Could you override the button's properties rather than disabling it - but disable interaction perhaps, or just change the colour/border of the button so that it retains your text colour? Just wondering if you have tried this fix, I know what you're saying though. – Luke Jun 18 '11 at 15:44
  • mmmm.. i guess i can set the image/alternate image to same "disabled" skin so user will understand that this is disabled. But it will make code very ugly - i would have to check in each action function if the button is disabled or not – Erik Sapir Jun 18 '11 at 15:50
  • In iOS, I can just do this: myButton.userInteractionDisabled = YES and then set the alpha to give it the appearance of being disabled. I imagine you can do something similar with your NSButton. Just to confirm, you were simply setting the enabled property to NO, which is where your text colour issue is coming from? – Luke Jun 18 '11 at 15:59
  • yes, i set the enabled property to know. I tried to find a way to just disable user interactions as in iOS, but with no luck. – Erik Sapir Jun 18 '11 at 16:05
  • The solution http://stackoverflow.com/a/10632311/594211 solves this as well. – jrc Aug 30 '12 at 13:21
  • Try `[btnInfo.cell setImageDimsWhenDisabled:NO];` from [this question.][1] [1]: http://stackoverflow.com/questions/11781483/dont-change-nsbutton-appearance-when-pressed-or-disabled – Alexander Aug 04 '14 at 02:58

3 Answers3

0

Subclass NSButtonCell and assign it to your button CELL in IB (not the BUTTON directly -> one level deeper). In the subclass implement the following and modifiy size, font and color as you like:

- (NSAttributedString*)attributedTitleForString:(NSString*)string
{
    // Colors
    NSColor *disabledColor = [NSColor grayColor];
    NSColor *enabledColor = [NSColor redColor];

    // Font
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setAlignment:NSLeftTextAlignment];
    NSFont *font = [NSFont systemFontOfSize:11];

    // Enabled
    NSDictionary *enabledAttrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                        enabledColor, NSForegroundColorAttributeName,
                                        style, NSParagraphStyleAttributeName,
                                        font, NSFontAttributeName,
                                        nil];
    NSAttributedString* enabledTitle = [[NSAttributedString alloc]initWithString:string attributes:enabledAttrsDictionary];

    // Disabled
    NSDictionary *disabledAttrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                         disabledColor, NSForegroundColorAttributeName,
                                         style, NSParagraphStyleAttributeName,
                                         font, NSFontAttributeName, nil];

    NSAttributedString* disabledTitle = [[NSAttributedString alloc]initWithString:string attributes:disabledAttrsDictionary];

    if (![self isEnabled])
        return disabledTitle;
    else
        return enabledTitle;
}

EDIT: Only works if setWantsLayers is false

Pat_Morita
  • 3,355
  • 3
  • 25
  • 36
0

You can set buttons properties (font, colors) for each state in IB. So would setting its text color for the disabled state to black help?

Christian Beer
  • 2,027
  • 15
  • 13
-1

On cocoa touch there is an API for that:

[myButton setTextColor:[UIColor blackColor] forState:UIControlStateDisabled];

For cocoa I don't know.

Kris Van Bael
  • 2,842
  • 1
  • 18
  • 19