Lable自适应高度方法的封装

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

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