NSAttributedString
extension NSAttributedString {
/// 获取文本高度
func getHeight(width: CGFloat) -> CGFloat {
let height = self.boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: [.usesLineFragmentOrigin], context: nil).size.height
return ceil(height)
}
/// 获取文本宽度
func getWidth(height: CGFloat) -> CGFloat {
let width = self.boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: height), options: [.usesLineFragmentOrigin], context: nil).size.width
return ceil(width)
}
}
String
extension String {
func getHeight(width: CGFloat, font: UIFont, lineSpacing: CGFloat = 0) -> CGFloat {
let attStr = NSMutableAttributedString(string: self)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = lineSpacing
attStr.addAttributes([NSAttributedString.Key.font : font, NSAttributedString.Key.paragraphStyle : paragraphStyle], range: NSRange(location: 0, length: attStr.length))
let height = attStr.boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: [.usesLineFragmentOrigin], context: nil).size.height
return ceil(height)
}
func getWidth(height: CGFloat, font: UIFont, lineSpacing: CGFloat = 0) -> CGFloat {
let attStr = NSMutableAttributedString(string: self)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = lineSpacing
attStr.addAttributes([NSAttributedString.Key.font : font, NSAttributedString.Key.paragraphStyle : paragraphStyle], range: NSRange(location: 0, length: attStr.length))
let width = attStr.boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: height), options: [.usesLineFragmentOrigin], context: nil).size.width
return ceil(width)
}
}