的UILabel sizeToFit不自动布局iOS6的工作 [英] UILabel sizeToFit doesn't work with autolayout ios6

查看:765
本文介绍了的UILabel sizeToFit不自动布局iOS6的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么配置编程(并且在这种方法)一个UILabel其高度依赖于它的文字?我一直在试图将其设置使用故事板和code的组合,但无济于事。每个人都建议 sizeToFit ,同时设置 lineBreakMode numberOfLines 。但是,不管我把code在 viewDidLoad中: viewDidAppear: viewDidLayoutSubviews 我无法得到它的工作。无论我做框长文本太小,它不会增长,或者我让过大,它不缩水。

How am I supposed to configure programmatically (and in which method) a UILabel whose height depends on its text? I've been trying to set it up using a combination of Storyboard and code, but to no avail. Everyone recommends sizeToFit while setting lineBreakMode and numberOfLines. However, no matter if I put that code in viewDidLoad:, viewDidAppear:, or viewDidLayoutSubviews I can't get it to work. Either I make the box too small for long text and it doesn't grow, or I make it too big and it doesn't shrink.

推荐答案

请注意,在大多数情况下的马特的解决方案如预期工作。但是,如果它不为你工作,请继续阅读。

Please note that in most of cases Matt's solution works as expected. But if it doesn't work for you, please, read further.

为了让您的标签自动调整高度,你需要做以下几点:

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


  1. 设置布局限制了标签

  2. 低优先级的设置高度约​​束。它应该比ContentCom pressionResistancePriority较低

  3. 设置numberOfLines = 0

  4. 设置ContentHuggingPriority比标签的高度更高的优先级

  5. 设置preferredMaxLayoutWidth的标签。该值用于标记来计算其高度

例如:

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_)]];

使用Interface Builder


  1. 设置了四个约束。高度约束是强制性的。

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

然后去标签的属性检查器并设置行数为0。

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

转到标签的大小和督察垂直增加和ContentHuggingPriority垂直ContentCom pressionResistancePriority。结果

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

选择和编辑高度约束。结果

Select and edit height constraint.

和降低高度约束的优先级。结果

And decrease height constraint priority.

享受。 :)

这篇关于的UILabel sizeToFit不自动布局iOS6的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆