NSString padding / space between characters

I am looking for an easy way in Obj.C to add a space between each character of my string. Thus, "1234" will look like "1 2 3 4".

I found a perfect javascript example here: stack overflow

Does anyone know something like this for Obj.C? Kerning is PITA on iOS, and ultimately that's all I need ...

Thoughts / comments?

Thanks! - Drew

+6
source share
3 answers

Try the following:

NSString *string =[NSString stringWithString:@"1234"]; NSMutableArray *buffer = [NSMutableArray arrayWithCapacity:[string length]]; for (int i = 0; i < [string length]; i++) { [buffer addObject:[NSString stringWithFormat:@"%C", [string characterAtIndex:i]]]; } NSString *final_string = [buffer componentsJoinedByString:@" "]; 
+4
source

To do it right, taking into account the problems mentioned in David Ronquist's comment, do something like this:

 NSMutableString* result = [origString mutableCopy]; [result enumerateSubstringsInRange:NSMakeRange(0, [result length]) options:NSStringEnumerationByComposedCharacterSequences | NSStringEnumerationSubstringNotRequired usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){ if (substringRange.location > 0) [result insertString:@" " atIndex:substringRange.location]; }]; 
+9
source

Do it:

  NSString *string =[NSString stringWithString:@"1234"]; NSMutableString *spacedString= [NSMutableString stringWithString:[NSString stringWithFormat:@"%C",[string characterAtIndex:0]]]; for(int i = 1; i<[string length];i++) { [spacedString appendString:[NSString stringWithFormat:@" %C",[string characterAtIndex:i]]]; } 
0
source

Source: https://habr.com/ru/post/918229/


All Articles