如何在 CALayer 上绘制看起来原生的状态项背景

发布于 2024-08-16 22:31:38 字数 951 浏览 7 评论 0原文

实际上,我想在自定义 statusItemView 的 CALayer 上绘制选定的 NSStatusItem 的背景。但由于

- (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 上绘制本机外观的状态项背景吗?

Actually I want to draw the background of a selected NSStatusItem on the CALayer of my custom statusItemView. But since

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

does not work (?) on layers I've tried it to draw the color with the backgroundColor property. But converting the selectedMenuItemColor into RGB doesn't help very much. It looks really plain without the gradient. :-/

I converted [NSColor selectedMenuItemColor] into a CGColorRef with this code:

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

Any idea how to draw a native looking statusitem background on a CALayer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里梦着梦中梦 2024-08-23 22:31:38
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];
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];
恏ㄋ傷疤忘ㄋ疼 2024-08-23 22:31:38

尝试子类化 CALayer 并实现 drawInContext: 方法为 CGContext 创建 NSGraphicsContext, 将 NSGraphicsContext 设置为当前上下文,然后告诉状态项绘制其背景。

Try subclassing CALayer and implementing the drawInContext: method to create an NSGraphicsContext for the CGContext, set the NSGraphicsContext as the current context, and then tell the status item to draw its background.

日记撕了你也走了 2024-08-23 22:31:38

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

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

I use this code in my layer delegate:

- (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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文