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

发布评论

分享到:

IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

iOS获取通讯录联系人信息详解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。