Lable自适应高度方法的封装
2018-07-20 来源:open-open
ios7---Lable自适应高度封装 +(UILabel *)getInfoLabel:(UILabel *)label withText:(NSString *)ktext withFont:(CGFloat )kfont withtosize:(CGRect)krect withBackGroundColor:(UIColor *)kbackgroundColor { //内容显示 高度自适应 CGSize sizeToFit =[ktext sizeWithFont:[UIFont systemFontOfSize:kfont]constrainedToSize:CGSizeMake(krect.size.width,10000)lineBreakMode:NSLineBreakByWordWrapping]; CGRect labelframe = CGRectMake(krect.origin.x,krect.origin.y,sizeToFit.width, sizeToFit.height); label.frame=labelframe; label.numberOfLines=0; label.lineBreakMode=NSLineBreakByTruncatingTail; label.font=[UIFont systemFontOfSize:kfont]; label.text = ktext; label.backgroundColor=kbackgroundColor; label.textAlignment = NSTextAlignmentLeft; label.textColor = [UIColor blackColor]; return label; } ios8---Lable自适应高度封装 +(UILabel *)getInfoLabel:(UILabel *)label withText:(NSString *)ktext withFont:(UIFont *)kfont withtosize:(CGRect)krect withBackGroundColor:(UIColor *)kbackgroundColor { label.numberOfLines =0; UIFont * tfont =kfont; label.font = tfont; label.lineBreakMode =NSLineBreakByTruncatingTail ; label.text =ktext; //高度估计文本大概要显示几行,宽度根据需求自己定义。 MAXFLOAT 可以算出具体要多高 CGSize size =CGSizeMake(krect.size.width,10000); NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil]; //ios7方法,获取文本需要的size,限制宽度 CGSize actualsize =[ktext boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:tdic context:nil].size; label.frame=CGRectMake(krect.origin.x,krect.origin.y,actualsize.width, actualsize.height); label.backgroundColor=kbackgroundColor; return label; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐