// MARK: - 獲得一個字符串的動態高度
convenience init(for bodyText: String, width: CGFloat) {
if (bodyText.characters.count ?? 0) <= 0 {
return 0
}
let cellFont = UIFont(name: "Helvetica", size: 15.0)
//UIFont *cellFont = [UIFont systemFontOfSize:13.f];
var paragraphStyle = NSMutableParagraphStyle.default.mutableCopy()
paragraphStyle.lineSpacing = 1
paragraphStyle.lineHeightMultiple = 1.0
/// Set line break mode
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping
/// Set text alignment
paragraphStyle.alignment = .natural
let attributesDictionary: [AnyHashable: Any] = [
NSFontAttributeName : cellFont!,
NSParagraphStyleAttributeName : paragraphStyle
]
let expectedLabelSize = bodyText.boundingRect(with: CGSize(width: width, height: FLT_MAX), options: .usesLineFragmentOrigin, attributes: (attributesDictionary as? [String : Any] ?? [String : Any]()), context: nil).size
集contrasint完美 – Jaydip