extension UILabel{
//根据text的字体大小和控件的宽度以及字体内容动态计算label的高度
func getTheHeightForLabel(font:UIFont,width:CGFloat,text:String) ->UILabel{
let label =UILabel()
let textW = width
label.text= text
label.font= font
label.numberOfLines=0
let textAtt:NSDictionary= [NSFontAttributeName: font]
let textSize:CGSize=CGSizeMake(textW,CGFloat(MAXFLOAT))
let textH:CGFloat= label.text!.boundingRectWithSize(textSize,options: .UsesLineFragmentOrigin,attributes: textAttas? [String:AnyObject],context:nil).size.height
label.frame=CGRectMake(0,0, textW, textH)
return label
}
网友评论