How can I use the \ t [tab] operator to format NSString in columns?

I would like to find a way to format NSString as follows:

Name: John Location: Unknown Type: Unknown Status: Unknown 

Now I use this code for this, but the result is completely different:

Code:

 serializedValue = [serializedValue stringByAppendingFormat:@"%@:\t\t%@\n",title, value]; 

Where title is the left column and value is the second.

Result:

 Name: John Location: Unknown Type: Unknown Status: Unknown 

Now, as you can see, adding always a constant number of tabs (\ t), 2 in my case, does not do the trick, because some words in the left column are longer and some are shorter. I am wondering if there is an easy way to do this that I do not know about.

+5
source share
1 answer
 NSString *titleColumn = [[NSString stringWithFormat:@"%@:", title] stringByPaddingToLength:20 withString:@" " startingAtIndex:0]; serializedValue = [serializedValue stringByAppendingFormat:@"%@%@", titleColumn, value]; 

Btw, it would be more efficient to use NSMutableString for serializedValue .

+9
source

All Articles