Is there a way to prevent the text from turning gray when I turn off the button?

When I turn off the buttons, the text turns gray (it used to be black). In my window, the result is that the text cannot be read when the button is disabled.

I looked through everything in the NSButton / NSButtonCell / NSCell / NSControl documentation, but I did not find a way to keep the text black. Do you know how I can do this?

+5
source share
3 answers

You can set the button properties (font, colors) for each state in IB. Thus, will the text color for the disabled state be set to black?

+1
source

NSButtonCell CELL IB ( BUTTON → ). , :

- (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: setWantsLayers false

0

Cocoa touch has an API for this:

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

For cocoa, I do not know.

-1
source

All Articles