// MARK: - 测算Label显示文字所需高度
/// 测算Label显示文字所需高度
///
/// - Parameters:
/// - width: 给定宽度
/// - font: 字号
/// - lineSpacing: 行间距
/// - kern: 字间距
/// - text: 文字
/// - Returns: 高度
public func getSpaceLabelHeight(byWith width: CGFloat, font: UIFont, lineSpacing: CGFloat, kern: CGFloat, text: String) -> CGFloat{
// 设置行间距
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = lineSpacing
// 设置字体/字间距
let attributes: [NSAttributedStringKey : Any] = [.paragraphStyle : paragraphStyle,
.font : font,
.kern : kern]
// 计算label字符串的高度
let rect: CGRect = text.boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)),
options: .usesLineFragmentOrigin,
attributes: attributes,
context: nil)
return rect.size.height
}
网友评论