yavfl 0.2.0

yavfl 0.2.0

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布上次发布2015年9月
SPM支持 SPM

MATSUMOTO Yuji 维护。



yavfl 0.2.0

  • MATSUMOTO Yuji

yavfl 是一个用于 Auto Layout 的可视化格式语言。

使用

在 yavfl 中,您可以像以下这样编写可视化格式语言的代码

visualFormat(label, textField) { l, t in
    .V ~ |-40-[l,==32]
    .V ~ |-40-[t,==32]
    .H ~ |-20-[l,==t]-10-[t]-|
}

原始可视化格式的等效代码如下

let dic = ["l": label, "t": textField]
label.setTranslatesAutoresizingMaskIntoConstraints(false)
textField.setTranslatesAutoresizingMaskIntoConstraints(false)
let c1 = NSLayoutConstraint.constraintsWithVisualFormat("V:|-40-[l(32)]", options: nil, metrics: nil, views: dic)
let c2 = NSLayoutConstraint.constraintsWithVisualFormat("V:|-40-[t(32)]", options: nil, metrics: nil, views: dic)
let c3 = NSLayoutConstraint.constraintsWithVisualFormat("|-20-[l(==t)]-10-[t]-|", options: nil, metrics: nil, views: dic)
self.view.addConstraints(c1)
self.view.addConstraints(c2)
self.view.addConstraints(c3)

更多示例

yavfl 原始
.H ~ [button]-[textField] [button]-[textField]
.H ~ [button,>=50] [button(>=50)]
.H ~ |-50-[purpleBox]-50-| |-50-[purpleBox]-50-|
.V ~ [topField]-10-[bottomField] V:[topField]-10-[bottomField]
.H ~ [maroonView]-0-[blueView] [maroonView][blueView]
.H ~ [button,==100~20] [button(100@20)]
.H ~ [button1,==button2] [button1(==button2)]
.H ~ [flexibleButton,>=70,<=100] [flexibleButton(>=70,<=100)]
.H ~ |-[find]-[findNext]-[findField,>=20]-| |-[find]-[findNext]-[findField(>=20)]-|

限制

  • Yavfl 不会检查语法有效性。即使编译成功,也不要期望代码是有效的。
  • “连接”的 predicateList 只支持一个 predicate
  • visualFormat 最多支持 5 个视图。

与原始可视化格式语言的差异

由于 Swift 的语法限制,一些语法部分已更改。

  • 不要使用 () 作为视图的 predicate
    • 使用 [button1,==button2] 而不是 [button1(==button2)]
  • 使用 ~ 而不是 : 作为 orientation,因为它们不能用于自定义运算符。
    • 使用 .H ~ |-[view]-| 而不是 H:|-[view]-|
    • 使用 [view,==100~200] 而不是 [view(==100@200)]
  • 应用布局约束时需要 Orientation (.H.V) 和运算符 ~
    • 运算符 ~ 是创建和应用约束的触发器。
  • 视图之间需要 连接 (-)。
    • 由于语法限制,不允许使用 [v1][v2]。请使用 [v1]-0-[v2]
  • 对于 predicate 需要 Relation (==<=>=)。
    • 不允许使用 [view,100]。请使用 [view,==100]

安装

pod 'yavfl'

或者,只需将 yavfl.swift 拷贝到您的项目中。