我在一个控制器中共享了NSControlPanel。我正在改变颜色到我的NSTextView背景和文本前景色。但是当我在面板中为背景更改颜色时,文本颜色也会自动更改。哪里有问题?谢谢回复!
- (IBAction)showColorPanel:(id)sender {
NSColorPanel *panel = [NSColorPanel sharedColorPanel];
[panel orderFront:nil];
[panel setAction:@selector(changeColorForBackground:)];
[panel setTarget:self];
[panel makeKeyAndOrderFront:self];
isFontPanel = NO;
}
- (void)changeColorForBackground:(id)sender {
if (!isFontPanel) {
DesktopController *desktopController = [self getDesktopController];
[desktopController updateCellBackgroundColor:[sender color]];
}
}
#pragma mark - font+color
- (IBAction)showFontPanel:(id)sender {
DesktopViewText *dvt = (DesktopViewText *)[[[self getDesktopController] startCell] subcell];
NSFontPanel *panel = [NSFontPanel sharedFontPanel];
NSFontManager *manager = [NSFontManager sharedFontManager];
if (dvt) {
[manager setSelectedFont:[[dvt label] font] isMultiple:NO];
}
[panel orderFront:nil];
[manager setAction:@selector(changeFont:)];
[manager setTarget:self];
isFontPanel = YES;
}
- (void)changeFont:(id)sender {
DesktopViewText *dvt = (DesktopViewText *)[[[self getDesktopController] startCell] subcell];
if (dvt) {
NSFont *oldFont = [dvt font];
NSFont *newFont = [sender convertFont:oldFont];
[[dvt textStorage] addAttribute:NSFontAttributeName value:newFont range:[dvt selectedRange]];
}
}
- (void)changeAttributes:(id)sender {
if (isFontPanel) {
DesktopViewText *dvt = (DesktopViewText *)[[[self getDesktopController] startCell] subcell];
if (dvt) {
NSDictionary *oldAttributes = [[dvt textStorage] fontAttributesInRange:[dvt selectedRange]];
NSDictionary *newAttributes = [sender convertAttributes: oldAttributes];
[[dvt textStorage] setAttributes:newAttributes range:[dvt selectedRange]];
//[[dvt textStorage] addAttribute:NSForegroundColorAttributeName value:[newAttributes ] range:[dvt selectedRange]];
}
}
}
我认为这是(void)changeAttributes:(id)发送者的错误所以我设置了断点。但是当我改变背景颜色时,还没有调用changeAttributes,所以没关系。
答案 0 :(得分:1)
问题只能位于-[DesktopController updateCellBackgroundColor:]
,因为看起来这是挑选颜色时调用的地方。由于无法访问此代码,我无法告诉您它如何更改您的模型。
补充说明和建议:
isFontPanel
变量的优点是什么? NSColorPanel
和NSFontPanel
采用足以区分的操作选择器。get
为getter添加前缀,这不遵循Cocoa约定,如果使用键值编码/观察,则很可能会失败。DesktopController
上定义方法,而不是直接更改其状态。