UILabel:如何在单词内添加连字符以换行或分段?

31

如何设置UILabel的lineBreakMode以打断单词并在打断的单词中添加连字符?

带有打断的单词的标签应该是这样的

w-

ord

6个回答

48

对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继承到被设置为属性文本的渲染中(您需要手动添加这些属性)。


3
我需要更多的内容。从苹果文档得知,“连字因子的有效值介于0.0到1.0之间(包括0.0和1.0)”。我将这个值降低以增加对连字符的抵抗力。 - lorenzo

30

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中

在此输入图片描述


需要注意的是,如果文本包含软连字符,则无需指定段落样式和断字系数。 - thibaut noah
要居中对齐,请将 paragraphStyle.alignment = .center 添加到其中。 - Martin Braun

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.
}];

1
“locale”是否在某处记录为NSAttributedString的有效属性名称?还有一个“NSLanguage”属性,即kCTLanguageAttributeName,在CoreText头文件中如下所述:值必须是包含区域设置标识符的CFStringRef。默认未设置。当将此属性设置为有效标识符时,它将用于选择本地化字形(如果字体支持)和特定于语言环境的断行规则。 - Stephan Tolksdorf
1
我从未测试过 NSLanguage 属性,也没有看到关于 @"locale" 的任何文档,但它肯定有效。 - bteapot

1

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

早期的UILabel没有提供断字功能。
今天,通过NSAttributedString(ios6+)可以实现。

请参见:IOS 6.x中的正字断字


你是否熟悉任何添加此功能的UILabel扩展? - David Ben Ari
UILabel现在支持使用NSAttributedStrings进行断字。 - bentford
谁把“唱歌”加到这个答案里的人是坏人。 - Richard Topchii

0

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.01.0之间的浮点数。因素为1.0将始终连字单词,而不管什么情况。因素为0.1只有在下一行没有足够的空间显示单词时才会连字。但是您可以调整因素以找到您喜欢的阈值。


0
这会对你有所帮助。请将UILabel文本平面更改为属性化 输入图像描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接