2

实际上我想在我的自定义 statusItemViewNSStatusItem上绘制一个选定的背景。CALayer但由于

- (void)drawStatusBarBackgroundInRect:(NSRect)rect withHighlight:(BOOL)highlight

在图层上不起作用(?)我尝试使用 backgroundColor 属性绘制颜色。但是将 selectedMenuItemColor 转换为 RGB 并没有多大帮助。没有渐变,它看起来很简单。:-/

我用这段代码转换[NSColor selectedMenuItemColor]成一个:CGColorRef

- (CGColorRef)highlightColor {
    static CGColorRef highlight = NULL;
    if(highlight == NULL) {
        CGFloat red, green, blue, alpha;
        NSColor *hlclr = [[NSColor selectedMenuItemColor] colorUsingColorSpace:
                     [NSColorSpace genericRGBColorSpace]];
        [hlclr getRed:&red green:&green blue:&blue alpha:&alpha];
        CGFloat values[4] = {red, green, blue, alpha};
        highlight = CGColorCreate([self genericRGBSpace], values);
    }
    return highlight;
}

知道如何在 CALayer 上绘制具有原生外观的状态项背景吗?

4

3 回答 3

3
NSImage *backgroundImage = [[NSImage alloc] initWithSize:self.frame.size]];
[backgroundImage lockFocus];
[self.statusItem drawStatusBarBackgroundInRect:self.bounds withHighlight:YES];
[backgroundImage unlockFocus];
[self.layer setContents:backgroundImage];
[backgroundImage release];
于 2012-04-14T06:21:08.433 回答
2

尝试继承 CALayer 并实现为 CGContext 创建 NSGraphicsContextdrawInContext:方法,将 NSGraphicsContext设置为当前 context,然后告诉状态项绘制其背景。

于 2010-01-08T23:15:28.410 回答
0

我在我的层委托中使用此代码:

- (void)drawLayer:(CALayer *)layer 
        inContext:(CGContextRef)context {
    NSGraphicsContext* gc = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO];
    [NSGraphicsContext saveGraphicsState];
    [NSGraphicsContext setCurrentContext:gc];
    [self.statusItem drawStatusBarBackgroundInRect:self.frame 
                                     withHighlight:self.isHighlighted];
    [NSGraphicsContext restoreGraphicsState];
}
于 2013-04-03T02:24:58.957 回答