当禁用按钮时,有什么方法可以防止文本变灰吗?

问题描述

| 当我将按钮设置为“禁用”时,文本变为灰色(之前为黑色)。 在我的窗口中,结果是禁用按钮时文本不可读。 我在NSButton / NSButtonCell / NSCell / NSControl的文档中查看了所有内容,但没有找到任何使文本保持黑色的方法。你知道我该怎么做吗?     

解决方法

        您可以在IB中为每个状态设置按钮属性(字体,颜色)。那么将其禁用状态的文本颜色设置为黑色有帮助吗?     ,        子类化NSButtonCell并将其分配给IB中的按钮CELL(而不是直接按钮->更深一层)。在子类中,根据需要实现以下内容和修改的大小,字体和颜色:
- (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,nil];

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

    if (![self isEnabled])
        return disabledTitle;
    else
        return enabledTitle;
}
编辑:仅在setWantsLayers为false时有效     ,        在可可粉上有一个API: [myButton setTextColor:[UIColor blackColor] forState:UIControlStateDisabled]; 对于可可我不知道。