2016-04-15 27 views
3

即時通訊嘗試使用LineSpacing屬性計算UILabel的高度。奇怪的是,正常label.text的高度的計算值低於label.attributedText及其lineheight。它看起來像我做錯了什麼,但無法找到什麼,所以請幫助:D。如何用線間距動態計算nsattributed字符串的高度

提供的代碼是專門爲SO編寫的,它使代碼緊湊和清晰,在我的項目中執行的方式不同。

extension NSAttributedString { 
    func heightWithWidth(width: CGFloat) -> CGFloat { 
     let maxSize = CGSize(width: width, height: CGFloat.max) 

     let boundingBox = self.boundingRectWithSize(maxSize, options: [.UsesLineFragmentOrigin, .UsesFontLeading, .UsesDeviceMetrics], context: nil) 

     return boundingBox.height 
    } 
} 

extension UILabel { 

    func getHeightWidthGivenWidthAndLineHeight(lineHeight: CGFloat, labelWidth: CGFloat) -> CGFloat { 
     let text = self.text 
     if let text = text { 
      let attributeString = NSMutableAttributedString(string: text) 
      let style = NSMutableParagraphStyle() 

      style.lineSpacing = lineHeight 
      attributeString.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSMakeRange(0, text.characters.count)) 
      let height = attributeString.heightWithWidth(labelWidth) 
      self.attributedText = attributeString 
      return height 
     } 
     return 0 
    } 

我通過

let contentHeight = contentLabel.text! == "" ? 0 : contentLabel.getHeightWidthGivenWidthAndLineHeight(3, labelWidth: labelWidth) 

調用此方法,正常工作的字符串(無間距)完美的作品,當我用lineSpacing如果失敗TE計算正確的值attributedstring。

回答

3

您可以使用UILabel的sizeThatFits。例如:

let text = "This is\nSome\nGreat\nText" 

    let contentHeight = contentLabel.text! == "" ? 0 : contentLabel.getHeightWidthGivenWidthAndLineHeight(6, labelWidth: labelWidth) 
    //returns 73.2 

但只設置

contentLabel.attributedText = contentLabel.attributedString //attributedString is same as getHeightWidthGivenWidthAndLineHeight 

    let size = contentLabel.sizeThatFits(contentLabel.frame.size) 
    //returns (w 49.5,h 99.5) 

代碼attributedString添加到您的擴展,如果你需要看到的是:

var attributedString:NSAttributedString?{ 
     if let text = self.text{ 
      let attributeString = NSMutableAttributedString(string: text) 
      let style = NSMutableParagraphStyle() 

      style.lineSpacing = 6 
      attributeString.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSMakeRange(0, text.characters.count)) 
      return attributeString 
     } 
     return nil 
    } 
+0

Thanx,不知道SizeThatFits()工作方式:)。我更新我的擴展以設置行高並通過使用SizeThatFits()返回uilabel所需的高度 – MQoder

0

我更新了我的擴展這種方式設置行高並同時返回新的標籤高度。 Thanx beyowulf

extension UILabel { 

    func setLineHeight(lineHeight: CGFloat, labelWidth: CGFloat) -> CGFloat { 
     let text = self.text 
     if let text = text { 
      let attributeString = NSMutableAttributedString(string: text) 
      let style = NSMutableParagraphStyle() 

      style.lineSpacing = lineHeight 
      attributeString.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSMakeRange(0, text.characters.count)) 
      self.attributedText = attributeString 
      return self.sizeThatFits(CGSize(width: labelWidth, height: 20)).height 
     } 
     return 0 
    } 
} 
相關問題