iphone - 如何使用\t [tab] 运算符在列中格式化 NSString?

标签 iphone objective-c text nsstring text-formatting

我想找到一种像这样格式化 NSString 的方法:

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

现在我正在使用这段代码来做,但结果却大不相同:

代码:

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

其中 title 是左列,value 是第二列。

结果:

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

现在,如您所见,始终添加恒定数量的制表符 (\t),在我的例子中为 2,并不能解决问题,因为左列中的某些单词较长,有些较短。我想知道是否有一种我不知道的简单方法可以做到这一点。

最佳答案

NSString *titleColumn = [[NSString stringWithFormat:@"%@:", title] stringByPaddingToLength:20 withString:@" " startingAtIndex:0];
serializedValue = [serializedValue stringByAppendingFormat:@"%@%@", titleColumn, value];

顺便说一句,为 serializedValue 使用 NSMutableString 会更有效。

关于iphone - 如何使用\t [tab] 运算符在列中格式化 NSString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6699930/

相关文章:

iphone - 如何设置字体大小以填充 UILabel 高度?

iphone - 我可以根据另一个单元格的状态显示/隐藏 UITableView 中的某个单元格吗?

ios - [[self.effectsTableView cellForRowAtIndexPath :indexPath]. textLabel text] 在 didSelectRowAtIndexPath 上返回 nil

html - 如何在图像上放置文本 block ?

iphone - 将 iPad 支持添加到 iPhone 项目 : universal vs two separate apps?

iphone - Phonegap 1.2在iPhone 3G iOS 3.x上不起作用

iphone - 在iPhone中单击按钮时出现“单击声音”的问题

iphone - 未调用 UITableView 委托(delegate)和数据源方法

linux - vi 文本搜索与排除 (linux)

java - 扫描文件并收集与模式匹配的完整单词