首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在CALayer上绘制本机外观的状态项背景

如何在CALayer上绘制本机外观的状态项背景
EN

Stack Overflow用户
提问于 2010-01-09 02:54:56
回答 3查看 1.3K关注 0票数 2

实际上,我想在自定义NSStatusItemCALayer上绘制选定statusItemView的背景。但是因为

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

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

我使用以下代码将[NSColor selectedMenuItemColor]转换为CGColorRef

代码语言:javascript
复制
- (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上绘制一个原生的状态项背景吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-01-09 07:15:28

尝试将CALayer子类化并将the drawInContext: method实现为create an NSGraphicsContext for the CGContextset the NSGraphicsContext as the current context,然后告诉状态项绘制其背景。

票数 2
EN

Stack Overflow用户

发布于 2012-04-14 14:21:08

代码语言:javascript
复制
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];
票数 3
EN

Stack Overflow用户

发布于 2013-04-03 10:24:59

我在我的层委托中使用了以下代码:

代码语言:javascript
复制
- (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];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2029917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档