0
我在一個控制器中共享NSControlPanel。我將顏色更改爲我的NSTextView背景和文本前景色。但是當我在面板中更改背景顏色時,文本顏色也會自動更改。問題在哪裏? Thx回覆!在一個控制器中共享NSColorPanel
- (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]];
}
}
}
我認爲這是錯誤的(無效)changeAttributes:(ID),發送方,所以我設置斷點。但是當我改變背景顏色時,changeAttributes沒有被調用,所以沒關係。
Thx for advices! updateCellBackgroundColor:看起來像這樣: - (void)updateCellBackgroundColor:(NSColor *)backgroundColor {self.startCell setBackgroundColor:backgroundColor]; [self.startCell setNeedsDisplay:YES]; } isFontPanel是用於識別NSPanel是NSColorPanel還是包含另一個NSColorPanel的NSFontPanel的變量。 – 2013-05-12 10:06:05