iOS的自动版式多行的UILabel [英] iOS AutoLayout multi-line UILabel

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

问题描述

以下问题是这个延续排序的:

Following question is sort-of continuation of this one:

的iOS:多行的UILabel的自动布局

主要的想法是,每一个观点是应该说明它的preferred(固有)的大小,这样可以自动版式知道如何正确显示。
的UILabel只是一种情况,一个视图的不能本身知道它需要什么样的显示尺寸的例子。这取决于提供什么样的宽度。

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.

由于 mwhuss 指出,集preferredMaxLayoutWidth 的那样进行跨多行标签跨度的把戏。但是,这是不是这里的主要问题。现在的问题是在哪里,什么时候我得到这个的宽度的价值,我作为参数发送到的集preferredMaxLayoutWidth

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.

在UIView的的

-(CGSize) intrinsicContentSize

我的集preferredMaxLayoutWidth 的根据self.frame.width我UILabels。

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

UIViewController的

UIViewController's

-(void) viewDidLayoutSubviews

是第一个回调方法我知道那里的主视图的子视图的任命,他们在屏幕上居住的确切帧。从这个方法我在里面,那么,操作上我的子视图,使UILabels被分为基于被任命为它们的宽度多行其内在的大小无效。

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.

推荐答案

这似乎烦人一个UILabel并非默认其宽度为preferred最大宽度的布局,如果你有约束被明确定义该宽度为您服务。

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.

在几乎每一个情况下,我已经使用了自动布局下的标签,则preferred最大宽度布局一直是标签的实际宽度,一旦我的布局的其余部分已经完成。

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.

因此​​,为了使这个自动发生,我已经使用一个UILabel子类,它覆盖的setBounds:。在这里,调用超实现,那么,如果这是不是这样就,设置preferred最大版式宽度是界限大小宽度。

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.

重点是重要的 - 设置preferred最大的布局会导致执行另一个布局传递,这样你就可以用一个无限循环结束。

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的自动版式多行的UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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