自定义NSButton具有半透明背景

时间:2011-12-29 01:03:29

标签: objective-c xcode macos nsbutton

我正在尝试创建一个带有50%不透明黑色背景和白色文本的自定义NSButton。为此,我已经将NSButton子类化并重载了DrawRect:

- (void) drawRect:(NSRect)dirtyRect
{

    [self setBordered:NO];

    //REMED since it has same effect as NSRectFill below
    //[[self cell] setBackgroundColor:[NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:0.2]];

    NSColor* backgroundColor = [NSColor colorWithCalibratedWhite:0 alpha:0.3f];
    [backgroundColor setFill];
    NSRectFill(dirtyRect);

    [super drawRect:dirtyRect];
}

白色文字显示正常,但按钮的背景始终是100%不透明。不解释alpha值。

有什么想法吗?谢谢!

2 个答案:

答案 0 :(得分:3)

NSRectFill()的默认操作是 copy ,这不是您想要的。替换为

NSRectFillUsingOperation(dirtyRect, NSCompositeSourceAtop);

答案 1 :(得分:0)

我发现的另一个解决方案是保持我的代码相同,但为Interface Builder中的每个按钮打开核心动画层。我对Core Animation Layer知之甚少,不知道为什么会这样。我之前关闭了CAL,因为它让我的字体看起来很锯齿。

相关问题