Lable自适应高度方法的封装

2015-05-23 0 232
Lable自适应高度方法的封装
    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;  
    }  

遇见资源网 objective-c Lable自适应高度方法的封装 http://www.ox520.com/16785.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务