Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

iphone - How can I use the [tab] operator to format a NSString in columns?

I would like to find a way to format a NSString like this:

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

Right now I'm using this code to do it but the result is quite different:

The code:

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

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

The result:

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

Now, as you can see, adding always a constant number of tabs (), 2 in my case, doesn't do the trick, because some words in the left column are longer and some are shorter. I'm wondering if there is a simple way to do this that I'm not aware of.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
NSString *titleColumn = [[NSString stringWithFormat:@"%@:", title] stringByPaddingToLength:20 withString:@" " startingAtIndex:0];
serializedValue = [serializedValue stringByAppendingFormat:@"%@%@", titleColumn, value];

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...