iOS 中 cell和 label 的自适应高度

    之前我们使⽤的 cell 一直都是同样的高度,或者某行固定⾼度。 实际开发中经常要让 cell 根据 Model 中文本的长短动态的更改高度
1、获取文本高度

    • 计算一段文本在限定宽高内所占矩形大⼩
      iOS7计算文本⾼度⽅法
      - (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context
      2、注意事项:
        1. 计算文本高度是所用的字体要和label显示时用的字体一致。
        2. label的宽度要和计算时使用的限定宽度一致。
        3. 这样才能保证文本显示在label中时,label高度恰巧够。
      3、cell 自适应高度
        1. tableView:heightForRowAtIndexPath:方法要⽐tableView:cellForRowAtIndexPath先执行。
        2. 所以要提前计算好每⾏行cell需要多少高度。
      主要代码:
                AppDelegate.m文件中:
      #import "AppDelegate.h"
      
      @interface AppDelegate ()
      
      @end
      
      @implementation AppDelegate
      - (void)dealloc
      {
          [_window release];
          [super dealloc];
      }
      
      
      
      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
          self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
          // Override point for customization after application launch.
          self.window.backgroundColor = [UIColor greenColor];
         
          // 创建一个 lebel
          UILabel *label = [[UILabel alloc] initWithFrame:(CGRectMake(50, 100, 300, 100))];
          label.backgroundColor = [UIColor whiteColor];
          label.font = [UIFont systemFontOfSize:20];
          NSString *str = @" -『为龙』-出自:《Axis Powers Hetalia》王耀中心同人志《为龙》手心上 亘古的月光 那道伤 笑而过的苍凉 翘首觐向 你伫立一方 是你生而为龙的狷狂 谨记你的姓名是炎黄 烽燧上 战地的残阳 断刃旁 岁月悄然的流淌 十二章纹 你遥祭四望 血脉奔腾的黄河长江 是你与生俱来的张扬 风雨打尽红墙和绿瓦 丹青留下明日的黄花 汉字里墨香温存的一笔一划  世代传承的表达 盛衰荣辱斑驳了脸颊 千载过后洗净了铅华 一直坚守的土壤在你脚下 至死不渝的回答 藏心上 亘古的月光 怀中殇 不再回眸的苍凉 翘首觐向 你伫立此方 谨记生而为龙的模样 谨记我的姓名是炎黄";
          label.text = str;
          label.numberOfLines = 0;
          [self.window addSubview:label];
         
          // 计算文字的高度
          // size 需要宽度 是之前定义的 label 的宽度
          // 高度 需要 给一个到不了的大数
          // NSStringDrawingUsesLineFragmentOrigin 按照文本的矩形区域 返回 高度
         
          // 构建一个字体大小的字典
          // 字体大小的字号 一定要跟定义 Label 一样
          NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:20] forKey: NSFontAttributeName];
         
          // 计算300宽 30字号的 label 的高度
          CGRect frame = [str boundingRectWithSize:CGSizeMake(300, 1000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
          NSLog(@"%f", frame.size.height);
         
          CGRect newFrame = label.frame;
          newFrame.size.height = frame.size.height;
          label.frame = newFrame;
         
          [self.window makeKeyAndVisible];
          return YES;
      }
      

      效果图如上所示
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值