2011-06-16 69 views
8

由于某些原因,当我的按钮被禁用时,文本颜色变为白色。 我希望它保持黑色 - 我该怎么做?通过下拉菜单更改状态正常,高亮,残疾人等NSButton - 在禁用模式下设置文本颜色

你可以做,在Interface Builder:

+0

你可能已经试过button.textcolor = [UIColor blackColor];但为了以防万一 – Radu 2011-06-16 12:01:22

回答

-3

可以为一个按钮的不同状态设置文本,图像,颜色,字体等。名单。

+0

我无法在IB中找到此选项 – 2011-06-16 12:27:58

+4

nsbutton颜色并非如此。 – 2012-04-20 13:15:04

+0

@ErikSapir - 既然你接受了这个答案,我想你会发现IB在哪里。你可以和我们其他人分享吗? – pepsi 2012-05-29 20:21:49

24

你也可以继承NSButtonCell并重写的方法:

- (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]; 
} 

这样,当按钮被禁用,文本将有文字相同颜色被启用按钮时。

+0

这似乎是最好的解决方案。 – jrc 2012-08-30 13:18:55

+0

谢谢!这个效果很好! – Tommy 2017-03-21 12:51:34

7

还检查了该

[btnInfo.cell setImageDimsWhenDisabled:NO]; 
+0

这适用于我。这是最好的答案。 – Mazyod 2013-08-13 03:29:22

+2

不适用于我,文字颜色变暗 – 2014-11-10 23:41:18

2

您可以覆盖NSButtonCell的私有方法:

- (BOOL)_textDimsWhenDisabled { 
    return NO; 
} 

- (BOOL)_shouldDrawTextWithDisabledAppearance { 
    return NO; 
} 

我填补了雷达的公共方法:rdar:// 19218619

0

更新为迅捷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) 
    } 

这将使文本属性与启用按钮时相同。