[SOLVED] character palette trashes attributes
[SOLVED] character palette trashes attributes
- Subject: [SOLVED] character palette trashes attributes
- From: Robert Clair <email@hidden>
- Date: Mon, 13 Dec 2004 09:58:41 -0500
"Solved" is a bit strong. Perhaps "worked around" would be better.
The problem: When inserting characters using the character palette,
something
trashes the attribute dictionary removing all the attributes except for
the font entry.
(Perhaps someone at Apple typed "setAttributes" where they meant
"addAttributes" ???)
The result is the new text is black, no matter what color you wanted.
The solution:
subclass NSTextView
add variables:
BOOL attributesLocked;
NSMutableDictionary* savedAttributes;
overide insertText
- (void)insertText:(id)aString
{
savedAttributes = [self typingAttributes];
attributesLocked = YES;
[super insertText: aString];
attributesLocked = NO;
}
make the text view its own delegate and implement
textViewDidChangeTypingAttributes
- (void)textViewDidChangeTypingAttributes:(NSNotification *)notification
{
NSRange selectedRange = [self selectedRange];
if ( attributesLocked )
{
if ( selectedRange.length == 0
&& selectedRange.location > 0 )
{
NSMutableDictionary* attributes =
[NSMutableDictionary dictionaryWithCapacity: 3];
NSColor* fillColor =
[savedAttributes objectForKey:
NSForegroundColorAttributeName];
if ( fillColor )
{
[attributes setObject: fillColor
forKey: NSForegroundColorAttributeName];
}
NSColor* strokeColor =
[savedAttributes objectForKey: NSStrokeColorAttributeName];
if ( strokeColor )
{
[attributes setObject: strokeColor
forKey: NSStrokeColorAttributeName];
}
NSNumber* strokeWidth =
[savedAttributes objectForKey: NSStrokeWidthAttributeName];
if ( strokeWidth )
{
[attributes setObject: strokeWidth
forKey: NSStrokeWidthAttributeName];
}
[[self textStorage] addAttributes: attributes
range: NSMakeRange( selectedRange.location -1, 1)];
}
}
}
For reasons that are unclear to me textViewDidChangeTypingAttributes
gets called twice during
insertText. It also gets called under lots of other circumstances where
you
don't want to mess with things, which is why all the locks and stuff.
If there's a better way, I'd love to here it.
Bob Clair
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden