6

有没有正字法连字的方法?

像这样的东西:

NSStiring *string = @"In June 2010 at the World Wide Developers Conference, Apple announced version 4 of Xcode during the Developer Tools State of the Union address.";
UILabel *label = [[UILabel alloc] init];
label.text = string;

如果框架 UILabel 会变窄,请获取下一个:

在 2010 年 6 月
的全球
开发者
大会上,Apple 在开发者 工具国情咨文 中
宣布
了 Xcode 的第 4 版 。



4

3 回答 3

19

使用 NSAttributedString 和 NSParagraphStyle,ahyphenationFactor为 1。

于 2013-05-11T23:14:57.537 回答
5

Objective-C

NSString *string = @"In June 2010 at the World Wide Developers Conference, Apple announced version 4 of Xcode during the Developer Tools State of the Union address.";
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.hyphenationFactor = 1.0;
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:string];
UILabel *label = [[UILabel alloc] init];
label.attributedText = attributedString;

斯威夫特 4

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.hyphenationFactor = 1.0
let attributedString = NSMutableAttributedString(string: "Your String", attributes: [NSAttributedStringKey.paragraphStyle:paragraphStyle])
self.yourLabel.attributedText = attributedString

斯威夫特 3

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.hyphenationFactor = 1.0
let attributedString = NSMutableAttributedString(string: "Your String", attributes: [NSParagraphStyleAttributeName:paragraphStyle])
self.yourLabel.attributedText = attributedString
于 2017-06-01T09:26:18.570 回答
0

这将对您有所帮助。请将 Uilabel Text plain 更改为 Attributed 我试过 Xcode 7+在此处输入图像描述

于 2016-07-01T13:42:28.910 回答