前言
本文主要是针对喜欢使用AutoLayout布局,并且采用的是Programmatically Creating Constraints的方式的朋友。
目的
在大多数UI布局里,label+image的组合算是非常常见的界面组合元素(如下图),
看似相同却又处处有自己的设计特色,如何避免重复机械性的编写这种布局呢。封装成自定义的视图,通过简单属性设置来完成期望的UI样式,是我们一致的想法。封装很简单,但这里这里另一个问题又出现了,由于图片的大小及文字长度的不同,视图大小如何设置就成了另一个问题。大多数时候,我们期望如果自定义视图如果能自己计算大小,问题就变得不是问题,布局工作也变得开心了,这也就是本文要解决的问题--“做一个能WrapContent的自定义控件”。WrapContent在android开发中属于一个非常普通的属性,但在iOS里却截然相反,除了系统级的几个控件(如下图)外,
自定义的控件都需要做额外的工作才可以实现。本文会从两种设计方式(selfSizing和FittingSize)实现我们的目的,相关的说明参考Autolayout PG Guide
selfSizing Controls
先看一下关于Views with Intrinsic Content Size的说明。
The intrinsic content size is based on the view’s current content. A label or button’s intrinsic content size is based on the amount of text shown and the font used. For other views, the intrinsic content size is even more complex. For example, an empty image view does not have an intrinsic content size. As soon as you add an image, though, its intrinsic content size is set to the image’s size.
intrinsicContentSize
The natural size for the receiving view, considering only properties of the view itself.
Views with Intrinsic Content Size
The following recipes demonstrate working with views that have an intrinsic content size. In general, the intrinsic content size simplifies the layout, reducing the number of constraints you need. However, using the intrinsic content size often requires setting the view’s content-hugging and compression-resistance (CHCR) priorities, which can add additional complications.
从上面的描述里我们可以得到一些有用的信息:
1.selfSizing的视图都存在intrinsic content size,而且大小是根据要展示的内容决定,图片、文字长度及文字大小
2.intrinsic content size大小仅考虑自定的属性,所以我们自定义的视图填充数据时,只能考虑通过自身的属性来设置,而不能通过暴露子视图的属性填充。
3.使用intrinsic content size必须考虑CHCR的priorities
4.自定义视图的属性更改后通过invalidateIntrinsicContentSize通知控件布局大小变更
基于以上信息,我们就可以达到我们的目的,并且写了一个测试类InsetsLabelView供大家参考,由于代码太长就不贴出来了。当然要想利用Autolayout写出优雅的布局约束需要经验的积累和缜密的思考,这里不再赘述。
FittingSize Controls
Intrinsic Content Size Versus Fitting Size
The intrinsic content size acts as an input to Auto Layout. When a view has an intrinsic content size, the system generates constraints to represent that size and the constraints are used to calculate the layout.The fitting size, on the other hand, is an output from the Auto Layout engine.It is the size calculated for a view based on the view’s constraints. If the view lays out its subviews using Auto Layout,then the system may be able to calculate a fitting size for the view based on its content.The stack view is a good example. Barring any other constraints, the system calculates the stack view’s size based on its content and attributes. In many ways, the stack view acts as if it had an intrinsic content size: You can create a valid layout using only a single vertical and a single horizontal constraint to define its position. But its size is calculated by Auto Layout—it is not an input into Auto Layout. Setting the stack view’s CHCR priorities has no effect, because the stack view does not have an intrinsic content size.If you need to adjust the stack view’s fitting size relative to items outside the stack view, either create explicit constraints to capture those relationships or modify the CHCR priorities of the stack’s contents relative to the items outside the stack.
从上面的描述里得到的信息:
1.The fitting size是基于视图的约束计算出大小。为了确保系统能正常计算出布局大小,自定义视图必须保证四条边必须与子视图之间存在约束。
2.文档里给我们举了一个例子--The stack view。scrollview利用Autolayout布局的实现也是采用了这种思路。
3.设置自定义视图的CHCR priorities是不起作用的,因为fittingSize视图不包含intrinsic content size
基于以上的思路写了一个测试类LabelLabelView供参考。
总结
本文主要是提供编写一个能warpContnet的自定义控件的可能性和思路,具体实现时请需要参考示例代码和多加练习。