我想知道如何以编程方式“紧缩字母间距”?
这个选项适用于在xib文件中创建的UILabel,并且有时非常方便。
我知道这个问题之前已经被问过,但我没有看到答案提到这在界面构建器中是否可用,所以我很好奇...
你可能在寻找这个内容:
@property(nonatomic) BOOL adjustsLetterSpacingToFitWidth
iOS6
中新加入的UILabel
属性。
allowsDefaultTighteningForTruncation: Bool
将其设置为true
。(默认为false
)
根据苹果文档:
当此属性设置为true时,标签在允许截断发生之前会紧缩其文本的字符间距。标签基于字体、当前行宽度、换行模式和其他相关信息自动确定最大收紧量。
https://developer.apple.com/reference/uikit/uilabel/1620533-allowsdefaulttighteningfortrunca
adjustsLetterSpacingToFitWidth
已经自iOS7起被弃用。
现在(自iOS 8起),您可能想要执行类似于以下操作:
NSAttributedString *as =
[[NSAttributedString alloc] initWithString:@"Kerninating all the strings"
attributes:@{NSKernAttributeName : @(-2.0)}];
label.attributedText = as;