| Index | Submit class | Submit snippet | Submission feed | List |

BSSingleLineFormatter

Language: Objective-C, Author: Michele Balistreri
License: Public domain

Is a formatter (subclass of NSFormatter) which removes the newlines encountered in input. This can be used to avoid newlines characters to get in NSTextFields, for example by copy&pasting.

BSSingleLineFormatter source preview

#import "BSSingleLineFormatter.h"
#import "NSCharacterSet+BSNewLineCharacterSet.h"


@implementation BSSingleLineFormatter

- (NSString *)stringForObjectValue:(id)anObject
{
  NSString *returnString = @"";
  NSString *tmpString = nil;
  
  NSScanner *scanner = [NSScanner scannerWithString:anObject];
  while([scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&tmpString])
    returnString = [returnString stringByAppendingString:tmpString];

  return returnString;
}

- (BOOL)getObjectValue:(id *)anObject forString:(NSString *)string errorDescription:(NSString **)error
{
  NSScanner *scanner = [NSScanner scannerWithString:string];
  NSString *tmpString = nil;
  *anObject = @"";
  while([scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&tmpString])
    *anObject = [*anObject stringByAppendingString:tmpString];
  return YES;
}

- (BOOL)isPartialStringValid:(NSString *)partialString newEditingString:(NSString **)newString errorDescription:(NSString **)error
{
  if([partialString rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]].location == NSNotFound)
    return YES;
  
  NSScanner *scanner = [NSScanner scannerWithString:partialString];
  NSString *tmpString = nil;
  *newString = @"";
    
  while([scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&tmpString])
    *newString = [*newString stringByAppendingString:tmpString];
  
  return NO;
}
@end

BSSingleLineFormatter header preview

#import <Cocoa/Cocoa.h>


@interface BSSingleLineFormatter : NSFormatter 
{

}

@end
Download Archive

Dependencies:

Download Dependencies

Compatible with:


Comments

Motti Shneor says:

Hmm..... beautiful code snippets. However, a little wrapper application that shows how to use them would be EXTREMELY helpful. The logic is pretty clear, but the know-how of integrating stuff into COCOA apps is not documented enough.

Motti Shneor says:

Hmm..... beautiful code snippets. However, a little wrapper application that shows how to use them would be EXTREMELY helpful. The logic is pretty clear, but the know-how of integrating stuff into COCOA apps is not documented enough.

Name

Website

Do you hate spammers? (Answer "Yes")