2011-12-29 50 views
1

我試圖用50%不透明的黑色背景和白色文本創建自定義NSButton。要做到這一點,我子類NSButton和超載的drawRect:具有半透明背景的自定義NSButton

- (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值不被解釋。

任何想法?謝謝!

回答

3

NSRectFill()的默認操作是拷貝這不是你想要的。與

NSRectFillUsingOperation(dirtyRect, NSCompositeSourceAtop); 
+0

這工作!儘管我必須關閉Interface Builder中的按鈕上的Core Animation Layer。 – 2011-12-30 04:36:33

0

另一種解決方案,我發現是保持我的代碼相同,但打開的Core Animation層在Interface Builder中的每個按鈕將其更換。我對Core Animation Layer知之甚少,不知道爲什麼這會起作用。之前我曾將CAL關閉,因爲它讓我的字體顯得非常鋸齒。