Re: attributedStringByTrimmingCharactersInSet: method?
Re: attributedStringByTrimmingCharactersInSet: method?
- Subject: Re: attributedStringByTrimmingCharactersInSet: method?
- From: Keith Blount <email@hidden>
- Date: Sun, 23 Jul 2006 07:16:45 -0700 (PDT)
Thanks for your reply - much appreciated.
I tried your idea, but the trouble with using the
string methods directly, such as in your example, is
that all of the formatting and attributes of the
attributed string get stripped, which is obviously
counter-productive.
For now I have created my own NSAttributedString
category that provides the methods I need as follows:
- (NSAttributedString
*)attributedStringByTrimmingCharactersInSet:(NSCharacterSet
*)set
{
NSMutableAttributedString *newStr = [[self
mutableCopy] autorelease];
NSRange range;
// First clear any characters from the set from the
beginning of the string
range = [[newStr string]
rangeOfCharacterFromSet:set];
while (range.length != 0 && range.location == 0)
{
[newStr replaceCharactersInRange:range
withString:@""];
range = [[newStr string]
rangeOfCharacterFromSet:set];
}
// Then clear them from the end
range = [[newStr string] rangeOfCharacterFromSet:set
options:NSBackwardsSearch];
while (range.length != 0 && NSMaxRange(range) ==
[newStr length])
{
[newStr replaceCharactersInRange:range
withString:@""];
range = [[newStr string] rangeOfCharacterFromSet:set
options:NSBackwardsSearch];
}
return [[[NSAttributedString alloc]
initWithAttributedString:newStr] autorelease];
}
- (NSAttributedString
*)attributedStringByTrimmingWhitespace
{
return [self
attributedStringByTrimmingCharactersInSet:[NSCharacterSet
whitespaceAndNewlineCharacterSet]];
}
This seems to work fine, though if anyone thinks this
should be done in a better or more graceful way, I
would be grateful to hear about it.
Thanks again and all the best,
Keith
--- "Adam R. Maxwell" <email@hidden> wrote:
>
> On Jul 23, 2006, at 03:44, Keith Blount wrote:
>
> > Hi,
> >
> > I want to trim the whitespace from either end of
> an
> > attributed string. I had assumed that there would
> be
> > an attributed variant of NSString's
> > -stringByTrimmingCharactersInSet: method, which
> would
> > make this job very easy, but there doesn't seem to
> be
> > one. Do I need to go and write my own method to do
> > this, or am I missing something really obvious?
>
> For stuff like this you generally need to operate on
> the characters.
> I think the easiest way would be to create a mutable
> copy of your
> attributed string and then modify its mutableString
> directly:
>
>
CFStringTrimWhitespace((CFMutableStringRef)[mutableAttributedString
>
> mutableString])
>
> NSMutableString doesn't have an equivalent of
> CFStringTrimWhitespace,
> and you don't want to create a new instance with
> stringByTrimmingCharactersInSet:.
>
> hth,
> Adam
>
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
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