Created
March 21, 2012 07:54
-
-
Save Shilo/2145531 to your computer and use it in GitHub Desktop.
A NSString category that allows spacing between characters.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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