Skip to content

Instantly share code, notes, and snippets.

@Shilo
Created March 21, 2012 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shilo/2145531 to your computer and use it in GitHub Desktop.
Save Shilo/2145531 to your computer and use it in GitHub Desktop.
A NSString category that allows spacing between characters.
@interface NSString (Spacing)
- (NSString *)stringWithSpacing:(int)spacing;
- (NSString *)stringWithSpacing:(int)spacing andSpacingString:(NSString *)spacingString;
@end
@implementation NSString (Spacing)
- (NSString *)stringWithSpacing:(int)spacing {
return [self stringWithSpacing:spacing andSpacingString:@" "];
}
- (NSString *)stringWithSpacing:(int)spacing andSpacingString:(NSString *)spacingString {
if (spacing == 0) return self;
int length = [self length];
NSMutableString *string = [[NSMutableString alloc] initWithCapacity:(length*(spacing+1)-spacing)];
for (int i=0; i<length; i++) {
[string appendString:[NSString stringWithFormat:@"%c", [self characterAtIndex:i]]];
if (i<length-1) {
for (int s=0; s<spacing; s++) {
[string appendString:spacingString];
}
}
}
return [string autorelease];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment