【问题标题】:How to add spacing between UILabel Words如何在 UILabel 单词之间添加间距
【发布时间】:2014-06-16 00:12:05
【问题描述】:

我想增加 UILabel 单词之间的空格。 从下面的代码中,我能够增加字符之间的空间,但我想为单词做这件事

 NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString:string];
[attrStr addAttribute:NSKernAttributeName value:@(4.0) range:NSMakeRange(0, attrStr.length)];

任何帮助或指针将不胜感激。

提前致谢

【问题讨论】:

  • 不要认为这是可能的。但是您可以做的是在单词之间插入“小空格字符”,或者为空格设置更大的字体可能会更有趣。或者你可能只为空间添加这个 kern 属性
  • ok,如何设置空格的紧缩属性?
  • 尝试使用 initialString = @"ABC DEF",设置 NSKernAttributeName 范围为 NSMakeRange(3,1)。有用吗?

标签: ios7 ios6 uilabel nsattributedstring


【解决方案1】:

用两个空格替换一个空格,试试这个棘手的方法:

NSString *str = [str stringByReplacingOccurrencesOfString:@" " withString:@"  "];

【讨论】:

  • 我会接受这个答案,因为我发现这是迄今为止最好的,不知道这是正确的方法,但它有效;)