• 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
Re: Tracking text attribute ranges in NSTextStorage
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Tracking text attribute ranges in NSTextStorage


  • Subject: Re: Tracking text attribute ranges in NSTextStorage
  • From: Knud Möller <email@hidden>
  • Date: Wed, 06 Apr 2005 11:04:12 +0100

Hi,

I'm doing something very similar in a project of mine. I don't know any automatic way of doing it, but what I do is this:

- I have a subclass of NSTextStorage called AnnotatedTextStorage. Where you have note objects, I have annotation objects.
- in AnnotatedTextStorage, I override replaceCharactersInRange:range withString:string. There is also another method for attributed stringes. These methods get called every time the content of the TextStorage changes:


- (void)replaceCharactersInRange: (NSRange)range withString: (NSString *)str
{
[embeddedString replaceCharactersInRange: range withString: str];
int lengthChange = [str length] - range.length;
[self edited: NSTextStorageEditedCharacters
range: range
changeInLength: lengthChange];

// now deal with the annotations, we may have to move them around:
[self fixAnnotations];
}


- fixAnnotations then goes through the whole TextStorage and resets the range for each Annotation object it finds. The code looks like this:

int index;
NSRange rangePointer;
id data;
while (index < range.location + range.length) {
data = [self attribute: AnnotationAttribute
atIndex: index
effectiveRange: &rangePointer];
if (nil != data) {
// we have an AnnotationAttribute at index. rangePointer is set accordingly
// reset the range of the Annotation object:
[data setRange: rangePointer];
// ...
}
index = index + rangePointer.length;
}


"range" in the code above is the range within the TextStorage we want to look at. So, in my case it's (0, length of TextStorage).

Hope that helps,
Knud

Hello,

Is there a way to track attribute ranges on an
attributed string? I am thinking of NSTextStorage. I
have notes associated with ranges of text in a text
view, and currently I am tracking all of these ranges
manually through textShouldChange... and
textDidChange: methods.

The notes are objects in themselves that store the
range of text to which they are associated. If I
created a custom text attribute such as
MYNoteAttribute that stored a note object as its value
class, is there any way that this object can also
automatically know/find out the range of text with
which it is associated?

I hope that makes sense...

Many thanks in advance for any help,
Keith

_______________________________________________ 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
  • Prev by Date: Re: WWDC 2005 - Is the value worth the price? (OT)
  • Next by Date: Re: ADC Core Data article
  • Previous by thread: Tracking text attribute ranges in NSTextStorage
  • Next by thread: Re: Tracking text attribute ranges in NSTextStorage
  • Index(es):
    • Date
    • Thread