如何在 CALayer 上绘制看起来原生的状态项背景
实际上,我想在自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试子类化 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.我在层委托中使用此代码:
I use this code in my layer delegate: