首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加NSMutableParagraphStyle lineSpacing导致文本不适合UILabel

添加NSMutableParagraphStyle lineSpacing导致文本不适合UILabel
EN

Stack Overflow用户
提问于 2015-04-26 01:57:25
回答 2查看 11.1K关注 0票数 8

我已经创建了一个普通的UILabel,并希望能够添加行间距到文本,这到UILabel。

虽然当我这样做时,它会影响adjustsFontSizeToFitWidth,并且它不再适合UILabel。

我用过的一些代码:

代码语言:javascript
复制
        var userQuestionsAnswer = UILabel(frame: CGRectMake(0, 0, 20, 20))
        userQuestionsAnswer.font = UIFont(name: Font.GothamBlack, size: 15)
        userQuestionsAnswer.numberOfLines = 0
        userQuestionsAnswer.adjustsFontSizeToFitWidth = true

        var style = NSMutableParagraphStyle()
        style.lineSpacing = view.frame.size.height * 0.021
        style.alignment = NSTextAlignment.Center
        let attributes = [NSParagraphStyleAttributeName : style]
        var answerText = "This is the answer"

        self.userQuestionsAnswer.attributedText = NSAttributedString(string: answerText!, attributes:attributes)

有没有人能告诉我为什么会这样,以及我是如何解决的?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-14 03:57:12

卸下NSMutableParagraphStyle即可正常工作。我不知道为什么,但是这个属性是破坏文本字体大小调整的原因。

票数 1
EN

Stack Overflow用户

发布于 2017-04-24 22:13:46

可能的解决方案是:

  1. 设置与paragraphStyle相关的属性(lineHeightMultiplier、对齐等)在storyboard\xib;(在更改label;
  2. create属性的属性文本之前,将paragrapshStyle属性添加到所需的属性;将attributedString;
  3. set attributedString添加为attributedText属性。

在不使用任何语法的情况下,它可能如下所示:

代码语言:javascript
复制
    func paragraphStyle(in attrString: NSAttributedString?) -> NSParagraphStyle? {
        return attrString?.attribute(NSParagraphStyleAttributeName, at: 0, effectiveRange: nil) as? NSParagraphStyle
    }

    let text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."

    let attributedText = NSMutableAttributedString(string: text, attributes: [
        NSFontAttributeName: UIFont.systemFont(ofSize: 20.0),
        NSForegroundColorAttributeName: UIColor.orange
    ])

    if let paragraphStyle = paragraphStyle(in: label.attributedText) {
        let textRange = NSMakeRange(0, text.characters.count)
        attributedText.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: textRange)
    }

    label.attributedText = attributedText

使用pod 'SwiftyAttributes‘和NSMutableAttributedString扩展:

代码语言:javascript
复制
import SwiftyAttributes

extension NSMutableAttributedString {
    func withParagraphStyle(from attrString: NSAttributedString?) -> NSMutableAttributedString {
        guard let attrString = attrString, let pStyle = attrString.attribute(.paragraphStyle, at: 0) else {
            return self
        }

        return self.withAttribute(pStyle)
    }
}

代码将为:

代码语言:javascript
复制
    let text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."

    label.attributedText = text.withAttributes([
            .font(.systemFont(ofSize: 20.0)),
            .textColor(.orange)
        ]).withParagraphStyle(from: label.attributedText)
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29868722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档