码迷,mamicode.com
首页 > 其他好文 > 详细

Lable自适应高度方法的封装

时间:2015-04-08 16:29:35      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:ios开发   uilabel   

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;
}


Lable自适应高度方法的封装

标签:ios开发   uilabel   

原文地址:http://blog.csdn.net/wangzhaobin/article/details/44941613

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!