Attribute ranges & string ranges
Attribute ranges & string ranges
- Subject: Attribute ranges & string ranges
- From: Jeremy Dronfield <email@hidden>
- Date: Mon, 2 Sep 2002 17:23:12 +0100
Hello All,
Given an NSAttributedString, what is the difference, if any, between the
range of a given substring and the range of its attributes? It seems
there must be a difference, because I can't get the following code to
work properly. I'm trying to implement a text finder which will pick up
style attributes - e.g. the words "find me" underlined but not italic.
This is the latest version of the experimental code I'm using (it's an
adaptation of TextEdit's TextFinder primitive), which is designed just
to test whether a found substring is underlined:
- (IBAction)findAction:(id)sender
{
NSString *textContents = [textView string];
unsigned textLength;
if (textContents && (textLength = [textContents length])) {
NSRange range;
unsigned options = 0;
if ([caseCheckbox state]) options |= NSCaseInsensitiveSearch;
range = [textContents findString:[findField stringValue]
selectedRange:[textView selectedRange] options:options wrap:YES];
if (range.length) {
id attributeValue;
NSNumber *underlined = [NSNumber
numberWithInt:NSSingleUnderlineStyle];
attributeValue = [[textView textStorage]
attribute:NSUnderlineStyleAttributeName atIndex:range.location
effectiveRange:nil];
if ([attributeValue isEqualToNumber:underlined]) {
[textView setSelectedRange:range];
[textView scrollRangeToVisible:range];
}
}
}
}
NB: -findString: calls an implementation of NStringTextFinding which
relies on -rangeOfString: for finding.
What makes me think there must be a difference between string/attribute
ranges is that the method only works if the distance between the
start-point of the search and the first match is not too great (say a
couple of hundred characters). Help.
-Jeremy
========================================
email@hidden // email@hidden
The Alchemy Pages:
- fractious fiction at
http://freespace.virgin.net/jeremy.dronfield
========================================
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.