如何设置一个UILabel lineBreakMode来打破单词并添加连字符到破碎的单词?
a label with a broken wo-
rd should look like this
解决方法
阐述马特在这里的答案:
https://stackoverflow.com/a/16502598/196358可以使用NSAttributedString和NSParagraphStyle来完成.见下文:
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.hyphenationFactor = 1.0f; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:titleString attributes:@{ NSParagraphStyleAttributeName : paragraphStyle }]; self.titleLabel.attributedText = attributedString;
这将导致标签在逻辑位置中断使用连字符断开.看起来不错,而且很简单.它需要iOS 6.0,但我只在7.0下尝试.