Re: Stripping newlines/blanks when pasting on an NSTextField
Re: Stripping newlines/blanks when pasting on an NSTextField
- Subject: Re: Stripping newlines/blanks when pasting on an NSTextField
- From: "Adam R. Maxwell" <email@hidden>
- Date: Sat, 28 Oct 2006 16:18:37 -0700
On Oct 28, 2006, at 15:59, Michele Balistreri wrote:
[...]
This is the code, maybe not the best way to strip white characters,
but trimming is not enough for me unfortunately. Indeed if someone
knows a better way (maybe a little more flexible), please advice.
Hi Michele,
I'd use a more robust character set definition; here's a category
method that I've used (modified in Mail, so beware of typos). With
that, you can make a mutable copy of your string, then use -[NSString
rangeOfCharacterFromSet:] and -[NSMutableString
deleteCharactersInRange:] to remove the newlines (probably in an
NSString category method).
hth,
Adam
@interface NSCharacterSet (NewlineSet)
+ (NSCharacterSet *)newlineCharacterSet;
@end
@implementation NSCharacterSet (NewlineSet)
+ (NSCharacterSet *)newlineCharacterSet;
{
static NSCharacterSet *newlineCharacterSet = nil;
if (nil == newlineCharacterSet) {
// This will be a character set with all newline characters
(including the weird Unicode ones)
CFMutableCharacterSetRef newlineCFCharacterSet = NULL;
// get all whitespace characters (does not include newlines)
newlineCFCharacterSet = CFCharacterSetCreateMutableCopy
(CFAllocatorGetDefault(), CFCharacterSetGetPredefined
(kCFCharacterSetWhitespace));
// invert the whitespace-only set to get all non-whitespace
chars (the inverted set will include newlines)
CFCharacterSetInvert(newlineCFCharacterSet);
// now get only the characters that are common to
kCFCharacterSetWhitespaceAndNewline and our non-whitespace set
CFCharacterSetIntersect(newlineCFCharacterSet,
CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline));
newlineCharacterSet = [(id)newlineCFCharacterSet copy];
}
return newlineCharacterSet;
}
@end
_______________________________________________
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