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. :)

最新文章

  1. C函数
  2. Browser GetImage
  3. iOS-申请开发证书流程
  4. equals方法,hashcode()方法
  5. 笔记三、apache搭建gitweb【转】
  6. CreateProcess error=206, The filename or extension is too long"的一个解决方案
  7. Android 技术用于汇总
  8. 揭开CSS3媒体查询迷雾(min-width和max-width)
  9. mysql中 出现No query specified
  10. Oracle启用scott用户
  11. Thinkphp5.0 多图上传名称重复BUG
  12. psp表格记录-
  13. Python实现进度条功能
  14. 解决com.microsoft.sqlserver.jdbc.SQLServerException: 该连接已关闭
  15. iOS开发-ViewController的生命周期和切换
  16. (三)Jsoup 使用选择器语法查找 DOM 元素
  17. js setInterval() 用法示例
  18. springsecurity 表达式一览
  19. Android Design Support Library 中控件的使用简单介绍(一)
  20. Appium+python自动化27-android特有的wait_activity【转载】

热门文章

  1. 一、初识Spring Boot框架
  2. 图片加载框架之fresco
  3. 利用java执行shell脚本
  4. HBase 参考信息
  5. php配置php-fpm启动参数及配置详
  6. Django与Cookie
  7. poj3264(简单线段树)
  8. [转帖]jdk8 Metaspace 调优
  9. java暂停线程
  10. 掌握Mybatis的核心配置文件