• 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: [NEWBIE] NSAttributedString
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Prev by Date: Cocoa, XML and Java
  • Next by Date: Re: Problem with IBOutlet
  • Previous by thread: [NEWBIE] NSAttributedString
  • Next by thread: Re: Setting the clip, double buffering
  • Index(es):
    • Date
    • Thread