Visual Format Language
Visual Format Syntax
The following are examples of constraints you can specify using the visual format. Note how the text visually matches the image.
-
Standard Space
-
[button]-[textField]
Width Constraint
-
[button(>=50)]
Connection to Superview
-
|-50-[orchidBox]-50-|
Vertical Layout
-
V:[topField]-10-[bottomField]
Flush Views
-
[maroonView][oceanView]
Priority
-
[button(100@20)]
Equal Widths
-
[button1(==button2)]
Multiple Predicates
-
[flexibleButton(>=70,<=100)]
A Complete Line of Layout
-
|-[find]-[findNext]-[findField(>=20)]-|
The notation prefers good visualization over completeness of expressibility. There are constraints that cannot be expressed in visual format syntax, although most of the constraints that are useful in real user interfaces can be. One useful constraint that cannot be expressed is a fixed aspect ratio (for example, imageView.width = 2 * imageView.height). To create such a constraint, you must useconstraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:.
Visual Format String Grammar
The visual format string grammar is defined as follows (literals are shown in code font; e denotes the empty string).
| Symbol | Replacement rule |
|---|---|
| <visualFormatString> | (<orientation>:)? (<superview><connection>)? <view>(<connection><view>)* (<connection><superview>)? |
| <orientation> |
|
| <superview> |
|
| <view> |
|
| <connection> | e| |
| <predicateList> | <simplePredicate>|<predicateListWithParens> |
| <simplePredicate> | <metricName>|<positiveNumber> |
| <predicateListWithParens> |
|
| <predicate> | (<relation>)?(<objectOfPredicate>)( |
| <relation> |
|
| <objectOfPredicate> | <constant>|<viewName> (see note) |
| <priority> | <metricName>|<number> |
| <constant> | <metricName>|<number> |
| <viewName> | Parsed as a C identifier. This must be a key mapping to an instance of |
| <metricName> | Parsed as a C identifier. This must be a key mapping to an instance of |
| <number> | As parsed by |
Note: For the objectOfPredicate production, a viewName is only acceptable if the subject of the predicate is the width or height of a view. That is, you can use [view1(==view2)] to specify that view1 and view2 have the same width.
If you make a syntactic mistake, an exception is thrown with a diagnostic message. For example:
Expected ':' after 'V' to specify vertical arrangement |
V|[backgroundBox]| |
^ |
A predicate on a view's thickness must end with ')' and the view must end with ']' |
|[whiteBox1][blackBox4(blackWidth][redBox]| |
^ |
Unable to find view with name blackBox |
|[whiteBox2][blackBox] |
^ |
Unknown relation. Must be ==, >=, or <= |
V:|[blackBox4(>30)]| |
^ |
视觉格式语言详解
本文介绍了一种用于布局和约束视图的视觉格式语言及其语法。通过直观的符号和连接符,开发者可以轻松定义用户界面中元素的位置和大小关系。文章列举了多个示例,如标准间距、宽度约束等,并解释了如何表达复杂的布局需求。
181

被折叠的 条评论
为什么被折叠?



