iOS AutoLayout 多行UILabel [英] iOS AutoLayout multi-line UILabel

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

问题描述

下面的问题是这个问题的延续:

iOS:自动布局中的多行 UILabelp>

主要思想是每个视图都应该声明它的首选"(固有)大小,以便 AutoLayout 可以知道如何正确显示它.UILabel 只是视图无法自己知道它需要什么尺寸来显示的情况的一个例子.这取决于提供的宽度.

正如 mwhuss 所指出的,setPreferredMaxLayoutWidth 做到了让标签跨越多行.但这不是这里的主要问题.问题是我在何时何地获得这个作为参数发送给 setPreferredMaxLayoutWidthwidth 值.

我设法制作了一些看起来合法的东西,如果我有任何错误,请纠正我,如果你知道更好的方法,请告诉我.

在 UIView 的

-(CGSize) intrinsicContentSize

我根据self.frame.width为我的UILabels setPreferredMaxLayoutWidth.

UIViewController的

-(void) viewDidLayoutSubviews

是我知道的第一个回调方法,主视图的子视图被指定为它们在屏幕上的确切帧.然后,从该方法内部,我对我的子视图进行操作,使它们的固有大小无效,以便 UILabel 根据指定给它们的宽度分成多行.

解决方案

如果您有明确定义该宽度的约束你.

在我使用 Autolayout 下的标签的几乎每一种情况下,一旦执行了其余的布局,首选的最大布局宽度就是标签的实际宽度.

因此,为了自动实现这一点,我使用了一个 UILabel 子类,它覆盖了 setBounds:.在这里,调用超级实现,然后,如果不是这样,将首选最大布局宽度设置为边界大小宽度.

重点很重要 - 设置首选最大布局会导致执行另一个布局传递,因此您最终可能会出现无限循环.

Following question is sort-of continuation of this one:

iOS: Multi-line UILabel in Auto Layout

The main idea is that every view is supposed to state it's "preferred" (intrinsic) size so that AutoLayout can know how to display it properly. UILabel is just an example of a situation where a view cannot by itself know what size it needs for display. It depends on what width is provided.

As mwhuss pointed out, setPreferredMaxLayoutWidth did the trick of making the label span across multiple lines. But that is not the main question here. The question is where and when do I get this width value that I send as an argument to setPreferredMaxLayoutWidth.

I managed to make something that looks legit, so correct me if I am wrong in any way and tell me please if you know a better way.

In the UIView's

-(CGSize) intrinsicContentSize

I setPreferredMaxLayoutWidth for my UILabels according to self.frame.width.

UIViewController's

-(void) viewDidLayoutSubviews

is the first callback method I know where subviews of the main view are appointed with their exact frames that they inhabit on the screen. From inside that method I, then, operate on my subviews, invalidating their intrinsic sizes so that UILabels are broken into multiple lines based on the width that was appointed to them.

解决方案

It seems annoying that a UILabel doesn't default to its width for the preferred max layout width, if you've got constraints that are unambiguously defining that width for you.

In nearly every single case I've used labels under Autolayout, the preferred max layout width has been the actual width of the label, once the rest of my layout has been performed.

So, to make this happen automatically, I have used a UILabel subclass, which overrides setBounds:. Here, call the super implementation, then, if it isn't the case already, set the preferred max layout width to be the bounds size width.

The emphasis is important - setting preferred max layout causes another layout pass to be performed, so you can end up with an infinite loop.

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

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