NSButton禁用标题颜色总是灰色 [英] NSButton disabled title color always gray

查看:1064
本文介绍了NSButton禁用标题颜色总是灰色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的NSButton,但无论我做什么,禁用的颜色总是灰色的



我试过所有的解决方案我遇到了




  • 我设置属性字符串标题与白色前景色(我看起来像禁用状态的颜色属性被忽略)

    / li>
  • 我已设置[[self cell] setImageDimsWhenDisabled:NO];






  //当禁用时,NSButtonCell的图像和文本通常用灰色调暗。 
//单选按钮和开关使用(imageDimsWhenDisabled == NO),所以只有他们的文本变暗。
@property BOOL imageDimsWhenDisabled;

不起作用



NSButton使用wantsUpdateLayer YES,所以绘制方法被覆盖,但我不明白,标题在哪里绘制

解决方案

OS X 10.9我通过子类化绘制按钮的单元格,禁用了按钮文本的颜色。



在Xcode中创建一个新的 NSButtonCell 子类,并覆盖以下方法:

   - (NSRect)drawTitle:(NSAttributedString *)title 
withFrame:(NSRect)frame
inView:(NSView *)controlView {

NSDictionary * attributes = [title attributesAtIndex:0 effectiveRange:nil];

NSColor * systemDisabled = [NSColor colorWithCatalogName:@System
colorName:@disabledControlTextColor];
NSColor * buttonTextColor = attributes [NSForegroundColorAttributeName];

if(systemDisabled == buttonTextColor){
NSMutableDictionary * newAttrs = [attributes mutableCopy];
newAttrs [NSForegroundColorAttributeName] = [NSColor orangeColor];
title = [[NSAttributedString alloc] initWithString:title.string
attributes:newAttrs];
}

return [super drawTitle:title
withFrame:frame
inView:controlView];

}

选择Xcode中的按钮,然后选择其单元格最容易在Interface Builder Dock中执行此操作),现在转到 Identity Inspector 并将单元格的类设置为子类的


I have a custom NSButton, but no matter what i do, the disabled color is always gray

I tried all solutions i came across

  • i'am setting the attributed string title with white foreground color (i looks like the color attribute is ignored for the disabled state)

  • i did set [[self cell] setImageDimsWhenDisabled:NO];

event when the documentations states

// When disabled, the image and text of an NSButtonCell are normally dimmed with gray.
// Radio buttons and switches use (imageDimsWhenDisabled == NO) so only their text is dimmed.
@property BOOL imageDimsWhenDisabled;

it doesn't work

My NSButton uses wantsUpdateLayer YES, so the draw methods are overwritten, but i don't understand, where the title is drawn

解决方案

On OS X 10.9 I've managed to alter the color of the button's text when it's disabled by sub-classing the cell that draws the button.

Create a new NSButtonCell subclass in Xcode and override the following method:

- (NSRect)drawTitle:(NSAttributedString *)title
      withFrame:(NSRect)frame
         inView:(NSView *)controlView {

    NSDictionary *attributes = [title attributesAtIndex:0 effectiveRange:nil];

    NSColor *systemDisabled = [NSColor colorWithCatalogName:@"System" 
                                                  colorName:@"disabledControlTextColor"];
    NSColor *buttonTextColor = attributes[NSForegroundColorAttributeName];

    if (systemDisabled == buttonTextColor) {
        NSMutableDictionary *newAttrs = [attributes mutableCopy];
        newAttrs[NSForegroundColorAttributeName] = [NSColor orangeColor];
        title = [[NSAttributedString alloc] initWithString:title.string
                                            attributes:newAttrs];
    }

     return [super drawTitle:title
              withFrame:frame
                 inView:controlView];

}

Select the button in Xcode, then select its cell (maybe easiest to do this in the Interface Builder dock), now got to the Identity Inspector and set the cell's class to that of your subclass.

这篇关于NSButton禁用标题颜色总是灰色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆