实际上,我想在自定义NSStatusItem的CALayer上绘制选定statusItemView的背景。但是因为
- (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上绘制一个原生的状态项背景吗?
发布于 2010-01-09 07:15:28
尝试将CALayer子类化并将the drawInContext: method实现为create an NSGraphicsContext for the CGContext、set the NSGraphicsContext as the current context,然后告诉状态项绘制其背景。
发布于 2012-04-14 14:21:08
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];发布于 2013-04-03 10:24:59
我在我的层委托中使用了以下代码:
- (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];
}https://stackoverflow.com/questions/2029917
复制相似问题