Hello,
I have certain custom text attributes that are used in my NSTextStorage to
which I would like to add temporary attributes via the NSLayoutManager. For
example, say I have a custom "NoteColor" text attribute associated with an
NSColor object ([text addAttribute:@"NoteColor value:[NSColor redColor]
range:range]); any range of text with this attribute I want to be drawn using a
temporary attribute - for instance, having its NSForegroundAttributeName using
the colour associated with the attribute.
What is the best and most efficient way of doing this?
In the past, I have reapplied the temporary attributes manually using
NSLayoutManager's -setTemporaryAttribute:... every time the text is changed,
for instance. That obviously isn't ideal, and more recently I have taken to
overriding NSLayoutManager's
-temporaryAttributesAtCharacterIndex:effectiveRange: to do this. For instance:
- (NSDictionary *)temporaryAttributesAtCharacterIndex:(unsigned)charIndex
effectiveRange:(NSRangePointer)effectiveCharRange
{
NSDictionary *result = [super
temporaryAttributesAtCharacterIndex:charIndex
effectiveRange:effectiveCharRange];
NSTextStorage *textStorage = [self textStorage];
NSUInteger textLength = [textStorage length];
if (charIndex >= textLength)
return result;
NSMutableDictionary *mutableResult = nil;
NSDictionary *attribs = [textStorage attributesAtIndex:charIndex
effectiveRange:NULL];
NSColor *color = [attribs objectForKey:@"NoteColor"];
if (color != nil)
{
mutableResult = [NSMutableDictionary
dictionaryWithDictionary:result];
[mutableResult setObject:color
forKey:NSForegroundColorAttributeName];
result = mutableResult;
}
return result;
}
(The above typed in my e-mail program as an example.)
However, this too seems incredibly inefficient. The above example would work
fine, but as soon as I've checked for three or four custom attributes, typing
slows down.
Is there a better way of doing this?
Thanks and all the best,
Keith
_______________________________________________
Cocoa-dev mailing list ([email protected])
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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]