Latest 0.2.0
Homepage https://github.com/safx/yavfl
License MIT
Platforms ios 8.0, osx 10.10, requires ARC
Frameworks Foundation
Authors

TravisCI
codecov.io
Platform
License
Version
Carthage

yavfl is Yet Anoter Visual Format Language for Auto Layout.

Usage

In yavfl, you can write the code of visual format language as follows:

visualFormat(label, textField) { l, t in
    .V ~ |-40-[l,==32];
    .V ~ |-40-[t,==32];
    .H ~ |-20-[l,==t]-10-[t]-|
}
  • Trailing ;s are needed since Xcode 7 beta 3 when you will write multiple statements in a block.

yavfl

The following code is the equivallent code of the original visual format:

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)

If you aren’t familiar with Visual Format Language, please visit the original Visual Format Language page first.

More Examples

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

Limitations

  • Yavfl does not check syntatic validity. You don’t expect the code is valid even if its compilation succeeded.
  • Connection‘s predicateList supports only one predicate.
  • function visualFormat supports up to 5 views.

Differences from the Original Visual Format Language

Some parts of the syntax are changed due to Swift’s syntax limitation.

  • Do not use () for view’s predicate.
    • Use [button1,==button2] instead of [button1(==button2)].
  • Use ~ instead of : for orientation and @ for priority, because these can not be used for custom operators.
    • Use .H ~ |-[view]-| instead of H:|-[view]-|.
    • Use [view,==100~200] instead of [view([email protected])].
  • Orientation (.H or .V) and operator ~ are required for applying layout constraints.
    • Operator ~ is a trigger for creating and applying constraints.
  • Connection (-) is required between views.
    • [v1][v2] is not allowed due to the syntax limitation. Use [v1]-0-[v2] instead.
  • Relation (==, <= or >=) is required for predicate.
    • [view,100] is not allowed. Use [view,==100] instead.

Install

pod 'yavfl'

Or, just copy yavfl.swift into your project.

Other Resources (in Japanese)

Latest podspec

{
    "name": "yavfl",
    "version": "0.2.0",
    "summary": "Yet Anoter Visual Format Language for Auto Layout in Swift",
    "homepage": "https://github.com/safx/yavfl",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "MATSUMOTO Yuji": "[email protected]"
    },
    "source": {
        "git": "https://github.com/safx/yavfl.git",
        "tag": "0.2.0"
    },
    "source_files": "yavfl.swift",
    "frameworks": "Foundation",
    "platforms": {
        "ios": "8.0",
        "osx": "10.10"
    },
    "requires_arc": true
}

Pin It on Pinterest

Share This