كيفية رسم خلفية الحالة الأصلية في كاليير
-
19-09-2019 - |
سؤال
في الواقع أريد رسم خلفية المحدد NSStatusItem
على ال CALayer
من الحالات المخصصة الخاصة بي. لكن منذ
- (void)drawStatusBarBackgroundInRect:(NSRect)rect withHighlight:(BOOL)highlight
لا يعمل (؟) على الطبقات التي جربتها لرسم اللون مع خاصية الخلفية. لكن تحويل المحللة المحددة إلى 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 والتنفيذ ال drawInContext:
طريقة ل إنشاء nsgraphicscontext ل cgcontext, اضبط nsgraphicscontext كسياق الحالي, ، ثم أخبر عناصر الحالة لرسم خلفيته.
نصائح أخرى
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];
يمكنني استخدام هذا الرمز في مندوب طبقة بلدي:
- (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];
}
لا تنتمي إلى StackOverflow