【发布时间】:2019-11-19 22:56:42
【问题描述】:
我正在尝试通过在NSViewController 中切换暗/亮模式来更改图像的颜色。
我正在使用此代码来更改图像的颜色:
- (NSImage *)image:(NSImage *)image withColour:(NSColor *)colour
{
NSImage *img = image.copy;
[img lockFocus];
[colour set];
NSRect imageRect = NSMakeRect(0, 0, img.size.width, img.size.height);
NSRectFillUsingOperation(imageRect, NSCompositingOperationSourceAtop);
[img unlockFocus];
return img;
}
我试过从viewWillLayout调用这个方法
self.help1Image.image = [self image:self.help1Image.image withColour:[NSColor systemRedColor]];
但系统颜色似乎总是返回相同的 RGB 值。
我也尝试过收听通知AppleInterfaceThemeChangedNotification,但即使在这里,RGB 值似乎也保持不变1.000000 0.231373 0.188235。
[[NSDistributedNotificationCenter defaultCenter] addObserverForName:@"AppleInterfaceThemeChangedNotification"
object:nil
queue:nil
usingBlock:^(NSNotification * _Nonnull note) {
NSLog(@"AppleInterfaceThemeChangedNotification");
self.help1Image.image = [self image:self.help1Image.image withColour:[NSColor systemRedColor]];
NSColorSpace *colorSpace = [NSColorSpace sRGBColorSpace];
NSColor *testColor = [[NSColor systemBlueColor] colorUsingColorSpace:colorSpace];
CGFloat red = [testColor redComponent];
CGFloat green = [testColor greenComponent];
CGFloat blue = [testColor blueComponent];
NSLog(@"%f %f %f", red, green, blue);
}];
我在 NSButtonCell sublass 和覆盖 layout 中工作正常,但无法在 NSViewController 中工作
【问题讨论】:
标签: macos nscolor macos-darkmode