27

How do I set a UILabel lineBreakMode to break words and add hyphens to broken words?

a label with a broken wo-

rd should look like this

4

6 回答 6

44

在这里详细说明马特的答案: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 下尝试过。

于 2013-10-16T21:57:04.587 回答
26

斯威夫特 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

斯威夫特 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

斯威夫特 3.0

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

从故事板

在此处输入图像描述

于 2017-03-04T05:19:08.700 回答
5

有时添加locale属性键至关重要。

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.
}];
于 2016-09-07T15:38:49.307 回答
1

我无法删除它,因为它已被接受,但从今天的 POV 来看我错了。

早期的 UILabel 没有提供连字符。
今天它通过 NSAttributedString (ios6+)

请参阅:IOS 6.x 中的正交连字词

于 2013-09-24T14:38:11.990 回答
0

这将对您有所帮助。请将 Uilabel Text 纯文本更改为 Attributed 在此处输入图像描述

于 2016-07-01T13:39:40.693 回答
0

斯威夫特 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.01.0无论如何,一个因素总是会连字这个词。如果下一行没有足够的空间来显示它而不使用连字符,一个因素0.1只会对单词进行连字符。但是您可以使用该因素来找到您喜欢的阈值。

于 2021-11-12T02:11:34.740 回答