Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.