Re: [NEWBIE] NSAttributedString
Re: [NEWBIE] NSAttributedString
- Subject: Re: [NEWBIE] NSAttributedString
- From: "Brian E. Webster" <email@hidden>
- Date: Thu, 5 Jul 2001 17:27:42 -0500 (CDT)
>
Is it possible to make an array of NSAttributedStrings from a single
>
NSAttributedString, as does NSString's componentsSeparatedByString ?
Anything is possible! Oh, I bet you were looking for an easy way, weren't
you? :) There's no built in method to magically do it, but it shouldn't
be too hard with the help of the attributedSubstringFromRange: method.
It'd go something like this:
@implementation NSAttributedString(Foo)
-(NSArray*)componentsSeparatedByString:(NSString*)separator
{
NSArray *stringComponents = [[self string]
componentsSeparatedByString:separator];
NSMutableArray *components = [[NSMutableArray alloc] init];
int i, separatorLength = [separator length];
NSRange range = NSMakeRange(-separatorLength, 0);
NSAttributedString *attributedString;
for(i = 0; i < [stringComponents count]; i++)
{
range.location = range.location + range.length +
separatorLength;
range.length = [[stringComponents objectAtIndex:i]
length];
attributedString = [self
attributedSubstringFromRange:range];
[components addObject:attributedString];
}
return [components autorelease];
}
@end
This is totally untested, and I'm sure there are more efficient ways to
do it, but something along these lines should work OK.
--
Brian Webster
email@hidden