Setting NSColorPanel to affect NSBackgroundColorAttributeName
Setting NSColorPanel to affect NSBackgroundColorAttributeName
- Subject: Setting NSColorPanel to affect NSBackgroundColorAttributeName
- From: Keith Blount <email@hidden>
- Date: Thu, 13 Dec 2007 03:43:44 -0800 (PST)
Hi,
I'm trying to implement to custom colour wells like the ones in Pages, whereby one affects the font colour of the text view and the other one affects the highlight/background colour of the text (set via NSBackgroundColorAttributeName). However, I am having some difficulties.
When a colour is selected in the shared NSColorPanel, a -changeColor: message is sent to the first responder - the text view. The standard text view's implementation of this is to change the NSForegroundColorAttributeName value. So, it seems to me that I need to override NSTextView's -changeColor: so that it can handle either a foreground or background change. In fact, I've done this, as follows:
- (void)setColorPanelChangesBackgroundColor:(BOOL)flag
{
colorPanelChangesBackgroundColor = flag;
}
- (BOOL)colorPanelChangesBackgroundColor
{
return colorPanelChangesBackgroundColor;
}
- (void)changeColor:(id)sender
{
if (![self colorPanelChangesBackgroundColor])
{
[super changeColor:sender];
return;
}
NSColor *color = [sender color];
if (![self isRichText])
{
NSBeep();
return;
}
NSArray *selection = [self rangesForUserCharacterAttributeChange];
NSValue *val;
NSRange selRange;
NSEnumerator *e = [selection objectEnumerator];
NSTextStorage *text = [self textStorage];
NSUndoManager *undo = [self undoManager];
while (val = [e nextObject])
{
selRange = [val rangeValue];
if ( ([selection count] == 1) && (selRange.length == 0) )
{
NSMutableDictionary *attribs = [NSMutableDictionary dictionaryWithDictionary:[self typingAttributes]];
[attribs setValue:color forKey:NSBackgroundColorAttributeName];
[self setTypingAttributes:attribs];
break;
}
if (selRange.length > 0)
{
if ([self shouldChangeTextInRange:selRange replacementString:nil])
{
[text addAttribute:NSBackgroundColorAttributeName
value:color
range:selRange];
[self didChangeText];
if (![undo isUndoing])
[undo setActionName:NSLocalizedString(@"Background Color",nil)];
}
}
}
// [self setColorPanelChangesBackgroundColor:NO];
}
The problem, however, is that once the flag is set to tell -changeColor: to affect the background colour, there is easy way of resetting (as in no obvious place to do this). So, when you select Format > Font > Show Colors, which you would expect to affect the foreground colour as it does in TextEdit, it will now affect the background colour if the background colour button has been used. If you reset the flag at the end of -changeColor:, the next click in the open colour panel after changing the background colour will change the foreground colour, which again, would not be expected.
So, my question is, what is the best way of doing this?
Thanks in advance for any help.
All the best,
Keith
____________________________________________________________________________________
Looking for last minute shopping deals?
Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden