content.boundingRectWithSize计算出来的高度不准

时间:2023-01-08 19:12:04
content.boundingRectWithSize计算出来的高度不准
 
计算出来的高度会少一行的高度,最后一行会显示不全。减掉padding会解决这个问题。
 
let padding = self.reviewText.textContainer.lineFragmentPadding
let actualWidth = DishReviewViewController.fullWidth - padding * 2
var height = UXHelper.calculateHeightToFitAttributedString(reviewText, width: actualWidth)
 
static func calculateHeightToFitAttributedString(content: NSAttributedString, width: CGFloat) -> CGFloat {
    return self.calculateSizeToFitAttributedString(content, width: width, height: CGFloat(FLT_MAX)).height
}

static func calculateSizeToFitAttributedString(content: NSAttributedString, width: CGFloat, height: CGFloat) -> CGSize {
    return content.boundingRectWithSize(CGSizeMake(width, height), options: [NSStringDrawingOptions.TruncatesLastVisibleLine, NSStringDrawingOptions.UsesLineFragmentOrigin], context:nil).size
}