如何设置UILabel的lineBreakMode以打断单词并在打断的单词中添加连字符?
带有打断的单词的标签应该是这样的
w-
ord
如何设置UILabel的lineBreakMode以打断单词并在打断的单词中添加连字符?
带有打断的单词的标签应该是这样的
w-
ord
对Matt的回答进行详细说明:https://dev59.com/mnLYa4cB1Zd3GeqPSQB6#16502598 可以使用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下尝试过。
警告:字体、颜色、对齐方式 --- 基本上没有任何属性从其宿主和/或所有者
UILabel
继承到被设置为属性文本的渲染中(您需要手动添加这些属性)。
Swift 5.0
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.hyphenationFactor = 1.0
let hyphenAttribute = [
NSAttributedString.Key.paragraphStyle : paragraphStyle,
] as [NSAttributedString.Key : Any]
let attributedString = NSMutableAttributedString(string: "Your String", attributes: hyphenAttribute)
self.yourLabel.attributedText = attributedString
Swift 4.0
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.hyphenationFactor = 1.0
let hyphenAttribute = [
NSAttributedStringKey.paragraphStyle : paragraphStyle,
] as [NSAttributedStringKey : Any]
let attributedString = NSMutableAttributedString(string: "Your String", attributes: hyphenAttribute)
self.yourLabel.attributedText = attributedString
Swift 3.0
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.hyphenationFactor = 1.0
let attributedString = NSMutableAttributedString(string: “Your String”, attributes: [NSParagraphStyleAttributeName:paragraphStyle])
self.yourLabel.attributedText = attributedString
从Storyboard中
paragraphStyle.alignment = .center
添加到其中。 - Martin Braunlocale
属性键是至关重要的。NSString *lorem = @"Lorem ipsum <et cetera>.";
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.hyphenationFactor = 1;
paragraph.alignment = NSTextAlignmentJustified;
paragraph.lineBreakMode = NSLineBreakByWordWrapping;
self.label.attributedText = [[NSAttributedString alloc] initWithString:lorem attributes:@{
NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleBody],
NSForegroundColorAttributeName: [UIColor darkGrayColor],
NSParagraphStyleAttributeName: paragraph,
@"locale": @"la", // Latin, use @"en-US" for American English, for example.
}];
kCTLanguageAttributeName
,在CoreText头文件中如下所述:值必须是包含区域设置标识符的CFStringRef。默认未设置。当将此属性设置为有效标识符时,它将用于选择本地化字形(如果字体支持)和特定于语言环境的断行规则。 - Stephan TolksdorfNSLanguage
属性,也没有看到关于 @"locale"
的任何文档,但它肯定有效。 - bteapot我无法删除它,因为它已被接受,但从今天的角度来看,我是错的。
早期的UILabel没有提供断字功能。
今天,通过NSAttributedString(ios6+)可以实现。
请参见:IOS 6.x中的正字断字。
Swift 5
class AttributedStrings {
private func paragraphStyle(alignment: NSTextAlignment, hyphenate: Bool) -> NSMutableParagraphStyle {
let style = NSMutableParagraphStyle()
style.hyphenationFactor = hyphenate ? 0.1 : 0
style.alignment = alignment
return style
}
func string(_ string: String, font: UIFont, color: UIColor, alignment: NSTextAlignment = .left, hyphenate: Bool = true) -> NSAttributedString {
let attributes: [NSAttributedString.Key: Any] = [
NSAttributedString.Key.font: font,
NSAttributedString.Key.foregroundColor: color,
NSAttributedString.Key.paragraphStyle: paragraphStyle(alignment: alignment, hyphenate: hyphenate)
]
return NSAttributedString(string: string, attributes: attributes)
}
}
let attributedStrings = AttributedStrings()
let attributedString1 = attributedStrings.string("Hyphenate this", font: .boldSystemFont(ofSize: 24), color: .black)
let attributedString2 = attributedStrings.string("Don't hyphenate this", font: .boldSystemFont(ofSize: 24), color: .black, hyphenate: false)
let attributedString3 = attributedStrings.string("Center and hyphenate this", font: .boldSystemFont(ofSize: 24), color: .black, alignment: .center)
let label = UILabel()
label.attributedText = attributedString1
由于我们无法对NSAttributedString
进行子类化,考虑创建一个供应商类来为您创建它们。我的答案的关键区别在于连字因素。连字因素是介于0.0
和1.0
之间的浮点数。因素为1.0
将始终连字单词,而不管什么情况。因素为0.1
只有在下一行没有足够的空间显示单词时才会连字。但是您可以调整因素以找到您喜欢的阈值。