extension String {
//////
/// @brief 获取字符串的高度
/// @param fontSize 字体大小
/// @param width 限制一行显示的宽度
/// @return 返回文本在width宽度的条件下的总高度
///
func height(let fontSize: CGFloat, let width: CGFloat) ->CGFloat {
let font = UIFont.systemFontOfSize(fontSize)
let size = CGSizeMake(width, CGFloat.max)
var style = NSMutableParagraphStyle()
style.lineBreakMode = NSLineBreakMode.ByCharWrapping
var attributes = [NSFontAttributeName: font, NSParagraphStyleAttributeName: style.copy()];
// 强转成NSString
var text = self as NSString
var rect = text.boundingRectWithSize(size,
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: attributes,
context: nil)
return rect.size.height
}
时间: 2024-11-17 23:16:06