• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSLayoutManager and best override point for temporary attributes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSLayoutManager and best override point for temporary attributes


  • Subject: NSLayoutManager and best override point for temporary attributes
  • From: Keith Blount <email@hidden>
  • Date: Tue, 29 Sep 2009 16:24:36 -0700 (PDT)

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@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

  • Follow-Ups:
    • Re: NSLayoutManager and best override point for temporary attributes
      • From: Martin Wierschin <email@hidden>
  • Prev by Date: Re: NSTableColumn value binding + keypath over a relationship
  • Next by Date: Re: Splitting controller logic
  • Previous by thread: Re: Core Data Calculated Properties [solved]
  • Next by thread: Re: NSLayoutManager and best override point for temporary attributes
  • Index(es):
    • Date
    • Thread