【问题标题】:How to set UILabel's line height?如何设置 UILabel 的行高?
【发布时间】:2021-11-18 23:53:24
【问题描述】:

正在谷歌中寻找答案
在 StackOverflow 上没有找到解决方案

Posting modified solution of this post

【问题讨论】:

    标签: ios swift uilabel height line


    【解决方案1】:

    添加这个扩展

    extension UILabel {
        func setLineHeight(lineHeight: CGFloat) {
            var attributeStringInitial: NSMutableAttributedString?
            var textInitial: String?
            
            if let text_ = self.text {
                attributeStringInitial = NSMutableAttributedString(string: text_)
                textInitial = text_
            } else if let text_ = self.attributedText {
                attributeStringInitial = NSMutableAttributedString(attributedString: text_)
                textInitial = text_.string
            }
            
            guard let attributeString = attributeStringInitial,
                  let text = textInitial
            else { return }
            
            let style = NSMutableParagraphStyle()
            
            style.lineSpacing = lineHeight
            attributeString.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: NSMakeRange(0, text.count))
            self.attributedText = attributeString
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-21
      • 2021-09-02
      • 1970-01-01
      • 2019-08-31
      • 1970-01-01
      • 1970-01-01
      • 2012-07-12
      • 2011-08-06
      • 1970-01-01
      相关资源
      最近更新 更多