To make your label automatically resize height you need to do following:

  1. Set layout constrains for label
  2. Set height constraint with low priority. It should be lower than ContentCompressionResistancePriority
  3. Set numberOfLines = 0
  4. Set ContentHuggingPriority higher than label's height priority
  5. Set preferredMaxLayoutWidth for label. That value is used by label to calculate its height

For example:

self.descriptionLabel = [[UILabel alloc] init];
self.descriptionLabel.numberOfLines = 0;
self.descriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.descriptionLabel.preferredMaxLayoutWidth = 200; [self.descriptionLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
[self.descriptionLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
[self.descriptionLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addSubview:self.descriptionLabel]; NSArray* constrs = [NSLayoutConstraint constraintsWithVisualFormat:@"|-8-[descriptionLabel_]-8-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(descriptionLabel_)];
[self addConstraints:constrs];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[descriptionLabel_]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(descriptionLabel_)]];
[self.descriptionLabel addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[descriptionLabel_(220@300)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(descriptionLabel_)]];

Using Interface Builder

  1. Set up four constraints. The height constraint is mandatory.

  2. Then go to the label's attributes inspector and set number of lines to 0.

  3. Go to the label's size inspector and increase vertical ContentHuggingPriority and vertical ContentCompressionResistancePriority.

  4. Select and edit height constraint.

  5. And decrease height constraint priority.

Enjoy. :)

Label 自适应文本(StoryBoard/xib)的更多相关文章

  1. 27、Label 自适应文本 xib

    第一步: 第二步: 第三步: 第四步:

  2. label自适应文本大小

    UILabel *label = [[UILabelalloc] initWithFrame:CGRectZero]; NSString *string = @"aa2fkoksdajfis ...

  3. 通过代码创建label 计算最佳尺寸 让其自适应文本高度或宽度

    通过xib创建label  让label随着文本内容的变化而动态改变尺寸比较简单  只要不固定死尺寸就可以了  但是通过代码创建的话我们需要给label设置一个frame  否则label默认是没有尺 ...

  4. iOS UILabel UITextView自适应文本,或文本大小自适应

    //UILabel自适应文本的高度 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(, , , )]; label.numberOf ...

  5. 浅析 - Storyboard / Xib

    大家都知道纯代码写应用的成本是很高的,特别是涉及到UI界面的实现,相当耗费时间.之前自己写应用时有了解过Storyboard,也简单使用过,但随着最近深入了解它之后,发现自己低估了它的作用和影响力,因 ...

  6. label自适应

    //label自适应 self.label = [UILabel new]; self.label.font = [UIFont systemFontOfSize:14]; NSString *tit ...

  7. iOS Label 自适应高度

    推荐第二个 测试一,只改变numberOfLines属性,label的高度不会自适应(会有text中的一部分内容称为......) NSString *str = @"jgreijgirje ...

  8. iOS开发笔记-根据frame大小动态调整fontSize的自适应文本及圆形进度条控件的实现

    最近同样是新App,设计稿里出现一种圆形进度条的设计,如下: 想了想,圆形进度条实现起来不难,但是其中显示百分比的文本确需要自适应,虽然可以使用时自己设定文本字体的大小,但是这样显得很麻烦,也很low ...

  9. TableView中Label自适应高度

    //Xcode6.3以后label自适应需要添加两个属性 _tableView.rowHeight = UITableViewAutomaticDimension; //给予预计行高 _tableVi ...

随机推荐

  1. mysq乱码问题

    不乱码的思想 liunx字符集→linux客户端字符集(例如:ssh)→mysql客户端字符集→mysql服务端字符集→库的字符集→表的字符集→程序字符集统一 mysql表跟库,库跟服务端字符集 li ...

  2. spring使用@Async注解异步处理

    1. 何为异步调用?在解释异步调用之前,我们先来看同步调用的定义:同步就是整个处理过程顺序执行,当各个过程都执行完毕,并返回结果. 异步调用则是只是发送了调用的指令,调用者无需等待被调用的方法完全执行 ...

  3. 重学Python - Day 06 - python基础 -> linux命令行学习 -- 简单基础命令学习

    学习资源 虚拟机工具:VMWare 12 linux :Ubuntu 14 或者CentOS 6 PS:ubuntu用远程连接工具的设置方法 step 1: 输入sudo apt-get instal ...

  4. configparser模块,subprocess 模块,xlrd,xlwt ,xml 模块,面向对象

    1. configparser模块 2.subprocess 模块 3.xlrd,xlwt 4.xml 模块 5.面向对象 面向对象是什么? 是一种编程思想,指导你如何更好的编写代码 关注点在对象 具 ...

  5. 【JulyEdu-Python基础】第 6 课:高级面向对象

    使用@property添加属性和自定义属性 __slots__和property 方法和属性的动态绑定 使用__slots__限定class实例能添加的属性 __slots__仅对当前类实例起作用,对 ...

  6. flutter环境配置(windows)

    参考以下链接 下载安装步骤: https://www.cnblogs.com/yangyxd/p/8809512.html 安装Flutter环境遇到Android license status un ...

  7. spring data 返回任意字段

    在spring boot + spring data查询数据库的过程中,有时候总会出现一些复杂的查询,我们希望数据库返回的字段能随意改变.这个需求在mybatis里很好解决,只需要用map接收就可以, ...

  8. linux shadow文件格式弱口令解密

    shadow格式弱口令为linux弱口令,通过kali linux 终端 john --w=字典 加上shadow文件, 扫描完成之后通过john --show 加上shadow文件出结果

  9. Oracle密码过期(the password has expired)

    1.进入sqlplus模式--sqlplus / as sysdba; 2.查看用户密码的有效期设置(一般默认的配置文件是DEFAULT) SELECT * FROM dba_profiles WHE ...

  10. [转帖]爬过这 6 个坡,你就能对 Linux 操作系统了如指掌

    爬过这 6 个坡,你就能对 Linux 操作系统了如指掌 http://www.51testing.com/html/16/n-4461316.html 学习的任务 任重道远 我现在处于第一阶段. 发 ...