• 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: Stripping newlines/blanks when pasting on an NSTextField
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Stripping newlines/blanks when pasting on an NSTextField (From: Michele Balistreri <email@hidden>)
 >Re: Stripping newlines/blanks when pasting on an NSTextField (From: Rosyna <email@hidden>)
 >Re: Stripping newlines/blanks when pasting on an NSTextField (From: "I. Savant" <email@hidden>)
 >Re: Stripping newlines/blanks when pasting on an NSTextField (From: Rosyna <email@hidden>)
 >Re: Stripping newlines/blanks when pasting on an NSTextField (From: "I. Savant" <email@hidden>)
 >Re: Stripping newlines/blanks when pasting on an NSTextField (From: Rosyna <email@hidden>)
 >Re: Stripping newlines/blanks when pasting on an NSTextField (From: Michele Balistreri <email@hidden>)
 >Re: Stripping newlines/blanks when pasting on an NSTextField (From: "I. Savant" <email@hidden>)
 >Re: Stripping newlines/blanks when pasting on an NSTextField (From: Michele Balistreri <email@hidden>)

  • Prev by Date: Re: Stripping newlines/blanks when pasting on an NSTextField
  • Next by Date: Re: Stripping newlines/blanks when pasting on an NSTextField
  • Previous by thread: Re: Stripping newlines/blanks when pasting on an NSTextField
  • Next by thread: Re: Stripping newlines/blanks when pasting on an NSTextField
  • Index(es):
    • Date
    • Thread