UILabel+FormattedText.swift
791 Bytes
//
// UILabel+FormattedText.swift
// metaCode
//
// Created by zx on 2024/1/5.
//
import Foundation
extension UILabel {
//计算label的行数
func getRealLabelTextLines() -> Int {
guard let labelText = text else {
return 0
}
//计算理论上显示所有文字需要的尺寸
let rect = CGSize(width: self.bounds.width, height: CGFloat.greatestFiniteMagnitude)
let labelTextSize = (labelText as NSString)
.boundingRect(with: rect, options:.usesLineFragmentOrigin,attributes: [NSAttributedString.Key.font: self.font!], context: nil)
//计算理论上需要的行数
let labelTextLines = Int(ceil(CGFloat(labelTextSize.height) / self.font.lineHeight))
return labelTextLines
}
}