zoukankan      html  css  js  c++  java
  • UITableView cell中label自动换行和自定义label自动换行

    换行的前提必须是有足够的高度 才能换 否则不显示超出部分 

    所以,在设置label换行的时候 要考虑cell的高度,cell的高度也要变化,废话不多说,来段代码:

    cell.label.text=[dict objectForKey:@"info"];
        cell.label.numberOfLines=0;  //可多行显示
        cell.label.lineBreakMode=NSLineBreakByWordWrapping;//拆行
    设置label的高度
    [self changeLabelHeight:cell.label :cell.label.text]; 
    
    //设置label的高度
    -(void)changeLabelHeight:(UILabel *)label : (NSString *)string{
        NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:15]};
        //计算UILabel字符显示的实际大小
         size=[string boundingRectWithSize:CGSizeMake(145, 1000)//最大限制的宽和高
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:attribute
                                              context:nil].size;
        //重设UILabel实例的frame
        [label setFrame:CGRectMake(label.frame.origin.x, label.frame.origin.y, size.width, size.height)];
       
        NSLog(@"%lf",size.height);
    }
    //设置cell的高度
    //设置行高
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:15]};
        NSDictionary *dict=[self.listData objectAtIndex:indexPath.row+1];
    //获取数据,根据数据来确定长和高
       CGSize size=[[dict objectForKey:@"info"] boundingRectWithSize:CGSizeMake(156, 1000)//最大限制宽和高
                                  options:NSStringDrawingUsesLineFragmentOrigin
                               attributes:attribute
                                  context:nil].size;
            NSLog(@"row heigth :%lf",size.height+20);
        if (size.height+20<58) {
            return 58;
        }else{
           return size.height+20;
        }
    }

  • 相关阅读:
    计算机网络的三种通讯模式(单播,广播,组播)
    java字符串面试题
    java使用纯命令行打包项目
    java字节码的工具(含IDEA插件)
    Spring配置之context:annotation与、component-scan以及annotation-driven
    Java ThreadLocal的使用案例
    对称平方数(to_string函数,stoi函数真香)
    字符串最后一位长度
    缺失的括号
    数三角形
  • 原文地址:https://www.cnblogs.com/niit-soft-518/p/4026118.html
Copyright © 2011-2022 走看看