UILabel sizeToFit 不适用于自动布局 ios6 [英] UILabel sizeToFit doesn't work with autolayout ios6

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

问题描述

我应该如何以编程方式(以及使用哪种方法)配置高度取决于其文本的 UILabel?我一直在尝试使用 Storyboard 和代码的组合来设置它,但无济于事.设置lineBreakModenumberOfLines 时,每个人都推荐sizeToFit.但是,无论我是否将该代码放在 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 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. 设置低优先级的高度约束.它应该低于 ContentCompressionResistancePriority
  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_)]];

使用界面生成器

  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 和垂直 ContentCompressionResistancePriority.

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天全站免登陆