Swift 单行文本的 UILabel 行距

Swift UILabel linespacing for single line text

我想增加 UI 标签单元格(顶部和底部)内的填充,并修改行高优先级。目前我使用该扩展名:

func setLineSpacing(lineSpacing: CGFloat = 0.0, lineHeightMultiple: CGFloat = 0.0) {

          guard let labelText = self.text else { return }

          let paragraphStyle = NSMutableParagraphStyle()
          paragraphStyle.lineSpacing = lineSpacing
          paragraphStyle.lineHeightMultiple = lineHeightMultiple

          let attributedString: NSMutableAttributedString
          if let labelattributedText = self.attributedText {
              attributedString = NSMutableAttributedString(attributedString: labelattributedText)
          } else {
              attributedString = NSMutableAttributedString(string: labelText)
          }

          // (Swift 4.2 and above) Line spacing attribute
          attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range:NSMakeRange(0, attributedString.length))
    
          print("attributed string \(attributedString)")
          self.attributedText = attributedString
      }

我的问题是,当 UILabel 的行数是单行时,它不会修改垂直间隙。因此,当设置了标签的特定行高时,我无法实现与 UI 设计相对应的特定偏移量。

paragraphStyle 感兴趣的是 Label 中的行,而不是整个组件。您正在此 lines(cells) 之间设置一些属性。因此,当行数为 1 时,affect.If 没有任何内容你想在文本(不是单元格)和 UILabel 组件之间给出顶部和底部,你必须将顶部和底部 space 给Label.

例子Class

class labelClass : UILabel{

var padding = UIEdgeInsets()

required init?(coder: NSCoder) {
    super.init(coder: coder)
}

func setLineSpacing(lineSpacing: CGFloat = 0.0, lineHeightMultiple: CGFloat = 0.0) {
    padding = UIEdgeInsets(top: lineSpacing, left: 0, bottom: lineSpacing, right: 0)
          guard let labelText = self.text else { return }

          let paragraphStyle = NSMutableParagraphStyle()
    
          paragraphStyle.lineSpacing = lineSpacing
          paragraphStyle.lineHeightMultiple = lineHeightMultiple
          let attributedString: NSMutableAttributedString
          if let labelattributedText = self.attributedText {
              attributedString = NSMutableAttributedString(attributedString: labelattributedText)
          } else {
              attributedString = NSMutableAttributedString(string: labelText)
          }

          // (Swift 4.2 and above) Line spacing attribute
          attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range:NSMakeRange(0, attributedString.length))
    
    
          print("attributed string \(attributedString)")
    
          self.attributedText = attributedString
    
 

      }

override func drawText(in rect: CGRect) {
    super.drawText(in: rect.inset(by: padding))
}

override var intrinsicContentSize : CGSize {
    let superContentSize = super.intrinsicContentSize
    let width = superContentSize.width + padding.left + padding.right
    let heigth = superContentSize.height + padding.top + padding.bottom
    return CGSize(width: width, height: heigth)
}

}

根据评论:

您可以使用

获取当前文本 numberOfLines 计数
extension UILabel {
func calculateMaxLines() -> Int {
    let maxSize = CGSize(width: frame.size.width, height: CGFloat(Float.infinity))
    let charSize = font.lineHeight
    let text = (self.text ?? "") as NSString
    let textSize = text.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
    let linesRoundedUp = Int(ceil(textSize.height/charSize))
    return linesRoundedUp
}
}

并在numberOfLines为1时进行运算

 func setLineSpacing(lineSpacing: CGFloat = 0.0, lineHeightMultiple: CGFloat = 0.0) {
    padding = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) // CHANGED

    guard let labelText = self.text else { return }
    
    let paragraphStyle = NSMutableParagraphStyle()
    
    paragraphStyle.lineSpacing = lineSpacing
    paragraphStyle.lineHeightMultiple = lineHeightMultiple
    let attributedString: NSMutableAttributedString
    if let labelattributedText = self.attributedText {
        attributedString = NSMutableAttributedString(attributedString: labelattributedText)
    } else {
        attributedString = NSMutableAttributedString(string: labelText)
    }
    
    // (Swift 4.2 and above) Line spacing attribute
    attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range:NSMakeRange(0, attributedString.length))
    
    
    
    self.attributedText = attributedString
    if self.calculateMaxLines() == 1 { // CHANGED
        padding = UIEdgeInsets(top: lineSpacing, left: 0, bottom: lineSpacing, right: 0)

    }
    
    
}