Swift 計算文字的size

devchena發表於2017-12-21

iOS 11之前限制寬高計算字串的size用的是UILabeltextRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect方法,當時也沒考慮執行緒安全問題(low爆了),Xcode也沒提示,用了好幾個版本,所幸一直都沒問題。

貼下方法(當時為什麼選這個方法就不解釋了):

func textSize(font: UIFont, constrainedSize: CGSize, lineSpacing: CGFloat?, lines: Int) -> CGSize {
        if self.isEmpty || lines < 0 {
            return CGSize.zero
        }
        
        let attributedString = NSMutableAttributedString(string: self)
        let range = NSRange(location: 0, length: attributedString.length)
        attributedString.addAttributes([NSFontAttributeName: font], range: range)
        if lineSpacing != nil {
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineBreakMode = .byTruncatingTail
            paragraphStyle.lineSpacing = lineSpacing!
            attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: range)
        }
        
        let calculatedLabel = UILabel()
        calculatedLabel.font = font
        calculatedLabel.attributedText = attributedString
        calculatedLabel.numberOfLines = lines
        let rect = calculatedLabel.textRect(forBounds: CGRect(x: 0, y: 0, width: constrainedSize.width, height: constrainedSize.height), limitedToNumberOfLines: lines)
        
        return rect.size
    }
複製程式碼

最近升級了Xcode 9,執行時警告我let calculatedLabel = UILabel()要在主執行緒執行,這時才意識到問題的嚴重性,馬上進行了修改:

extension String {

   func boundingRect(with constrainedSize: CGSize, font: UIFont, lineSpacing: CGFloat? = nil) -> CGSize {
        let attritube = NSMutableAttributedString(string: self)
        let range = NSRange(location: 0, length: attritube.length)
        attritube.addAttributes([NSAttributedStringKey.font: font], range: range)
        if lineSpacing != nil {
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = lineSpacing!
            attritube.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraphStyle, range: range)
        }
        
        let rect = attritube.boundingRect(with: constrainedSize, options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil)
        var size = rect.size
        
        if let currentLineSpacing = lineSpacing {
            // 文字的高度減去字型高度小於等於行間距,判斷為當前只有1行
            let spacing = size.height - font.lineHeight
            if spacing <= currentLineSpacing && spacing > 0 {
                size = CGSize(width: size.width, height: font.lineHeight)
            }
        }
        
        return size
    }
    
    func boundingRect(with constrainedSize: CGSize, font: UIFont, lineSpacing: CGFloat? = nil, lines: Int) -> CGSize {
        if lines < 0 {
            return .zero
        }
        
        let size = boundingRect(with: constrainedSize, font: font, lineSpacing: lineSpacing)
        if lines == 0 {
            return size
        }

        let currentLineSpacing = (lineSpacing == nil) ? (font.lineHeight - font.pointSize) : lineSpacing!
        let maximumHeight = font.lineHeight*CGFloat(lines) + currentLineSpacing*CGFloat(lines - 1)
        if size.height >= maximumHeight {
            return CGSize(width: size.width, height: maximumHeight)
        }
        
        return size
    }
}
複製程式碼

引數解釋 constrainedSize:限制的size font:字號 lineSpacing:預設為nil,使用系統預設的行間距 lines:限制的行數 注:程式碼版本為Swift 4.0

上面的兩個方法分別取代名:方法1和方法2。 方法1:限制寬高,可設定行間距,計算準確 方法2:比方法1多了限制行數功能。


配合使用UILabel的擴充套件方法:

extension UILabel {
    
    // 設定`numberOfLines = 0`的原因:
    // 配合方法`func boundingRect(with constrainedSize: CGSize, font: UIFont, lineSpacing: CGFloat? = nil, lines: Int) -> CGSize`使用,可以很好的解決不能正常顯示限制行數的問題;
    // 如果為label設定了限制行數(大於0的前提),使用上面的計算方法(帶行間距),同時字串的實際行數大於限制行數,這時候的高度會使label不能正常顯示。
    func setText(with normalString: String, lineSpacing: CGFloat?, frame: CGRect) {
        self.frame = frame
        self.numberOfLines = 0
                
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineBreakMode = .byTruncatingTail
        if lineSpacing != nil {
            if (frame.height - font.lineHeight) <= lineSpacing! {
                paragraphStyle.lineSpacing = 0
            } else {
                paragraphStyle.lineSpacing = lineSpacing!
            }
        }
        let attributedString = NSMutableAttributedString(string: normalString)
        let range = NSRange(location: 0, length: attributedString.length)
        attributedString.addAttributes([NSAttributedStringKey.font: font], range: range)
        attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraphStyle, range: range)

        self.attributedText = attributedString
    }
}

複製程式碼

在此感謝倉鼠:iOS 行距全攻略github.com/zhengwenmin…

相關文章