swift 计算label中文字高度
2018-05-12 本文已影响0人
艾欧尼亚
计算label的文字高度:
func getTextHeigh(textStr : String, font : UIFont, width : CGFloat) -> CGFloat{
let normalText : NSString = textStr as NSString
let size = CGSize(width: width, height:1000) //CGSizeMake(width,1000)
let dic = NSDictionary(object: font, forKey : kCTFontAttributeName as! NSCopying)
let stringSize = normalText.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: dic as? [NSAttributedStringKey:Any], context:nil).size
return stringSize.height
}