• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
wanghong
  • 首页
  • 新随笔
  • 订阅
  • 管理

IOS UILabel 根据内容自适应高度

iOS Label 自适应高度  适配iOS7以后的版本

更多

    self.contentLabelView = [[UILabel alloc] init];

    self.contentLabelView.font = SYS_FONT(15);

    self.contentLabelView.lineBreakMode =NSLineBreakByTruncatingTail ;

    self.contentLabelView.textColor =  [UIColor colorWithHexString:@"#444444"];

    self.contentLabelView.text =[@"12312312312312321321dddsdadsadasdasdas" stringByAppendingString:@"\n\n\n\n\n\n\n"];

    [self.contentLabelView setNumberOfLines:0];

    

    //根据内容计算出label所需要的高度

    CGSize size = CGSizeMake(kScreenWidth - expectSizes.width-20, MAXFLOAT);

    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:self.contentLabelView.font,NSFontAttributeName,nil];

    CGSize  actualsize =[self.contentLabelView.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin  attributes:tdic context:nil].size;

    self.contentLabelView.frame =CGRectMake(expectSizes.width+20, 254, actualsize.width, actualsize.height);

 

纯代码布局可能会用到,不过推荐使用xib或storyboard。

posted @ 2016-08-02 16:15  wanghongwin  阅读(1592)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3