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. Workflow-产品:泛微工作流引擎

    ylbtech-Workflow-产品:泛微工作流引擎 1.返回顶部 1. 工作流引擎平台技术架构 TECHNOLOGY FRAMEWORK 高度协同系统各应用模块 泛微工作流引擎平台是整个协同办公平 ...

  2. Django之模型的高级用法

    from django.db import models class Publisher(models.Model): name = models.CharField(max_length=30) a ...

  3. 五十七:flask文件上传之使用flask-wtf验证上传的文件

    1.安装:pip install flask-wtf2.定义表单验证的时候,对文件的字段,需使用:FileField3.验证器从flask_wtf.file中导入,FileRequired为验证文件必 ...

  4. php下关于字符串与数组的相互转换的函数

    public static function string2array($tags)    {        return preg_split('/\s*,\s*/',trim($tags),-1, ...

  5. SqlServer:SqlServer(存储过程动态表查询(取消返回值),事务处理,批量还原sqlserver备份,强制删除被占用的数据库)

    1.存储过程动态表查询 USE [NETWORKING_AUDIT] GO /****** Object: StoredProcedure [dbo].[impConfigInfo] Script D ...

  6. Sqlserver实现故障转移 — 加域(2)

    目的:将计算机添加到域中, 域控的建立详见:https://www.cnblogs.com/xiaoerlang90/p/9224745.html 域控: 名称:dcTest.com IP: 192. ...

  7. 串的应用与kmp算法讲解--学习笔记

    串的应用与kmp算法讲解 1. 写作目的 平时学习总结的学习笔记,方便自己理解加深印象.同时希望可以帮到正在学习这方面知识的同学,可以相互学习.新手上路请多关照,如果问题还请不吝赐教. 2. 串的逻辑 ...

  8. 网络实验 04-利用三层交换机实现VLAN间路由

    利用三层交换机实现VLAN间路由 一.实验目标 掌握交换机Tag VLAN 的配置 掌握三层交换机基本配置方法 掌握三层交换机VLAN路由的配置方法 通过三层交换机实现VLAN间相互通信 二.实验背景 ...

  9. 数据库中TOP—N查询

    1)查询1-4的数据 SELECT rownum, sno, cno, score FROM (SELECT * FROM sc order by score desc) WHERE rownum & ...

  10. 【AMAD】salabim -- Python中进行离散事件模拟

    简介 用法 个人评分 简介 salabim1是用来定义离散事件模拟(DES2),以及转换为动画的一个python库. 用法 请看官方文档3. 个人评分 实用性是基于对平均群众的,大多数人还是接触不到这 ...