iOS UILable自适应撑高即UITableViewCell的高度自适应改变

来源:互联网 发布:大金焓湿图计算软件 编辑:程序博客网 时间:2024/05/16 06:50

         UILable的自适应撑高,好久就使用这个相关的技术,就是一直没有时间总结这个知识点,今天终于有时间总结这一块的内容了,lable的自适应撑高是根据计算,将lable相关的内容进行填充的过程,这个过程我封装了一个方法,只要UILable调用即可,代码段如下:       

#pragma mark - lable自适应高度的计算- (CGRect)adaptiveLabelFrame:(UILabel *)label font:(UIFont *)font{    CGSize size =CGSizeMake(300,20000);    CGSize  actualsize;        if (IS_IOS7) {        //    获取当前文本的属性        NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil];        //ios7方法,获取文本需要的size,限制宽度        actualsize =[label.text boundingRectWithSize:size                                             options:NSStringDrawingUsesLineFragmentOrigin                                          attributes:tdic                                             context:nil].size;            } else {        actualsize= [label.text sizeWithFont:font                           constrainedToSize:size                               lineBreakMode:NSLineBreakByCharWrapping];            }        CGRect rect = label.frame;    rect.size = actualsize;    return rect;}
           创建一个Lable仅仅需要将此lable调用这个方法即可,Lable的创建如下代码段:

        _showContentLable = [[UILabel alloc] initWithFrame:self.bounds];        _showContentLable.numberOfLines = 0;        _showContentLable.font = [UIFont fontWithName:@"Arial" size:12];        _showContentLable.text = @"中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则国强中国强则";        _showContentLable.backgroundColor = [UIColor whiteColor];        self.lableAdativeHeight = [self adaptiveLabelFrame:_showContentLable font:_showContentLable.font];        _showContentLable.frame = self.lableAdativeHeight;        NSLog(@"---%f,%f,%f,%f---",self.lableAdativeHeight.origin.x,self.lableAdativeHeight.origin.y,self.lableAdativeHeight.size.width,self.lableAdativeHeight.size.height);        [self.contentView addSubview:_showContentLable];

最后实现效果如下图:

           OK,自适应撑高就是如此简单。。。。。。。。。
0 0
原创粉丝点击