Skip to content

Instantly share code, notes, and snippets.

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;
@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];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment