iOS版:多行的UILabel的自动布局 [英] iOS: Multi-line UILabel in Auto Layout

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

问题描述

我在遇到麻烦努力实现与自动布局一些非常基本的布局行为。我的视图控制器看起来像这样在IB:

I'm having trouble trying to achieve some very basic layout behavior with Auto Layout. My view controller looks like this in IB:

顶部标签是标签的标题,我不知道有多少行是。我需要的标题标签来显示文本的所有行。我还需要另外两个标签和小的图像正下方的标题进行布局,但高大恰好是。我已经设置了标题标签和它的父和小图像和它的父之间的间距底部之间的约束顶级间距约束标签和小图像之间垂直间隔的限制,以及。白色的UIView没有高度约束,所以应该垂直拉伸包含其子视图。我已为标题标签的行数设定为0

The top label is the title label, I don't know how many lines it will be. I need the title label to display all lines of text. I also need the other two labels and the small image to be laid out right below the title, however tall it happens to be. I have set vertical spacing constraints between the labels and small image, as well as a top spacing constraint between the title label and its superview and a bottom spacing constraint between the small image and its superview. The white UIView has no height constraint, so it should stretch vertically to contain its subviews. I have set the number of lines for the title label to 0.

我怎样才能得到标题标签,调整大小以适应由串所需行数?我的理解是,我不能使用SETFRAME方法,因为我使用的自动布局。而且我必须使用自动布局,因为我需要其他的意见留在标题标签(因此约束)以下。

How can I get the title label to resize to fit the number of lines required by the string? My understanding is that I can't use setFrame methods because I'm using Auto Layout. And I have to use Auto Layout because I need those other views to stay below the title label (hence the constraints).

我怎样才能做到这一点?

How can I make this happen?

推荐答案

使用 -set preferredMaxLayoutWidth 的UILabel 和自动布局应该处理的其余部分。

Use -setPreferredMaxLayoutWidth on the UILabel and autolayout should handle the rest.

[label setPreferredMaxLayoutWidth:200.0];

查看<一个href=\"http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UILabel_Class/Reference/UILabel.html#//apple_ref/occ/instp/UILabel/$p$pferredMaxLayoutWidth\">UILabel文档。

更新:

只需要在故事板的高度限制设置为大于或等于,无需设置preferredMaxLayoutWidth。

Only need to set the height constraint in storyboard to Greater than or equal to", no need to setPreferredMaxLayoutWidth.

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

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