我正在尝试通过切换暗/亮模式来改变图像的颜色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
子类和重写layout
但无法让它工作NSViewController