Share
Embed
Visual Format Languageが 簡単に書けるSwiftライブラリ yavfl
iOS, OS XでAuto LayoutのVisual Format Languageを簡潔に書けるようにするためのライブラリyavfl (https://github.com/safx/yavfl) の実装を簡単に紹介します。
matuyuji
April 11, 2015
Transcript
45 slides
-
2ͭͷViewΛಉ͡෯ͰԣʹฒΔ Visual Format Language 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: Yet Another VFL visualFormat(label, textField) { l, t in
.V ~ |-40-[l,==32] .V ~ |-40-[t,==32] .H ~ |-20-[l,==t]-10-[t]-| } Ϳ ; Δ -
Visual Format Language 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) -
ಛ • Visual Format Language෩ͷݴޠ • (VFLͷ) PredicatePriorityͳͲʹҰԠରԠ • ίϯύΠϧ࣌ʹνΣοΫ͞ΕΔͷͰ҆৺
• XcodeͰͷิ • Mac, iOS྆ରԠ • CocoaPodsͰ؆୯ಋೖ • Swift 1.2ඇରԠ -
yavflͷॲཧ (֓ཁ) • SwiftͷݴޠػೳΛ͍Ζ͍Ζͬͯɺ • Visual Format LanguageͷจࣈྻΛ࡞ͬͯɺ • NSLayoutConstraintͷ
constraintsWithVisualFormat:options:metrics:views Λద༻͢Δ͚ͩ ςετ͕ɺZBWqͷදهˠ7'-ͷදهͷ ม͕ਖ਼͍͜͠ͱͷνΣοΫ͚ͩͰࡁΉ -
yavflͰͬͨSwiftͷݴޠػೳ • Trailing Closures • enum • Custom Operators •
LiteralConvertible • Build Configurations -
Trailing Closures visualFormat(label, textField) { l, t in .V ~
|-40-[l,==32] .V ~ |-40-[t,==32] .H ~ |-20-[l,==t]-10-[t]-| } func visualFormat(v1: UIView, v2: UIView, closure:(ViewExpression, ViewExpression) -> ()) -
visualFormat func visualFormat(v1: YAVView, v2: YAVView, closure: (ViewExpression, ViewExpression) ->
()) { v1.yav_setTranslatesAutoresizingMaskIntoConstraints(false) v2.yav_setTranslatesAutoresizingMaskIntoConstraints(false) closure(.View(LayoutViewName(v1, 1)), .View(LayoutViewName(v2, 2))) v1.updateConstraints() v2.updateConstraints() } -
visualFormat(label, textField) { l, t in .V ~ |-40-[l,==32] .V
~ |-40-[t,==32] .H ~ |-20-[l,==t]-10-[t]-| } { l, t in .V ~ |-40-[l,==32] .V ~ |-40-[t,==32] .H ~ |-20-[l,==t]-10-[t]-| } -
visualFormat(label, textField) { l, t in .V ~ |-40-[l,==32] .V
~ |-40-[t,==32] .H ~ |-20-[l,==t]-10-[t]-| } .H ~ |-20-[l,==t]-10-[t]-| enum -
enum enum LayoutOrientation : String { case V = "V"
case H = "H" } -
Visual Format String Grammarʹج͍ͮͨΛఆٛ enum VisualFormat { case Superview case
View(LayoutView) case Connection case Predicate(LayoutPredicate) case Number(Int) case Composition([VisualFormat]) case Options(NSLayoutFormatOptions) } | [l] - 20 ==t -
IntegerLiteralConvertible enum VisualFormat : IntegerLiteralConvertible { case Superview case View(LayoutView)
case Connection case Predicate(LayoutPredicate) case Number(Int) case Composition([VisualFormat]) case Options(NSLayoutFormatOptions) } let vf: VisualFormat = 3 IntegerLiteralConvertible case Number(Int) public init(integerLiteral value: IntegerLiteralType) { self = .Number(value) } } -
enum VisualFormat : IntegerLiteralConvertible, ArrayLiteralConvertible { case Superview case View(LayoutView)
case Connection case Predicate(LayoutPredicate) case Number(Int) case Composition([VisualFormat]) case Options(NSLayoutFormatOptions) public init(integerLiteral value: IntegerLiteralType) { self = .Number(value) } public init(arrayLiteral elements: ViewExpression...) { self = .View(LayoutView(elements)) } } nshipster.com/swift-literal-convertible Swift Literal Convertibles let w1: ViewExpression = .View(LayoutViewName(v1, 1)) let w2: ViewExpression = .View(LayoutViewName(v2, 2)) let vf: VisualFormat = [w1,==w2] -
visualFormat(label, textField) { l, t in .V ~ |-40-[l,==32] .V
~ |-40-[t,==32] .H ~ |-20-[l,==t]-10-[t]-| } .H ~ |-20-[l,==t]-10-[t]-| (ArrayLiteralConvertible) VisualFormat (IntegerLiteralConvertible) VisualFormat LayoutOrientation -
Custom Operators • લஔ (prefix), ޙஔ (postfix), தஔ (infix) •
༏ઌॱҐ (precedence) • ݁߹ੑ (associativity) a + b * c a ~ (b ~ c) (a ~ b) ~ c a ~ b ~ c ++a a++ a + b a + (b * c) (a + b) * c -
visualFormat(label, textField) { l, t in .V ~ |-40-[l,==32] .V
~ |-40-[t,==32] .H ~ |-20-[l,==t]-10-[t]-| } .H ~ |-20-[l,==t]-10-[t]-| prefix postfix infix -
prefix operator |- {} public prefix func |- (e: VisualFormat)
-> VisualFormat { return VisualFormat(composition: .Superview, .Connection, e) } .H ~ |-20-[l,==t]-10-[t]-| .Number(20) -
.H ~ |-20-[l,==t]-10-[t]-| .H ~ |- - - - -|
20 l 10 t - | 20 - t | l 10 .H ~ - - - - | 20 - t | l 10 .H ~ - - - public func -(lhs: VisualFormat, rhs: VisualFormat) -> VisualFormat { return VisualFormat(composition: lhs, .Connection, rhs) } -
.H ~ |-20-[l,==t]-10-[t]-| public func ~(lhs: LayoutOrientation, rhs: VisualFormat) ->
[AnyObject] { if let superView = rhs.superView? { let exp = lhs.description + ":" + rhs.description let dic = rhs.viewsDictionary let opts = rhs.options let c = NSLayoutConstraint.constraintsWithVisualFormat(exp, options: opts, metrics: nil, views: dic) superView.addConstraints(c) return c } return [] } - | 20 - t | l 10 .H ~ - - - H:|-20-[l(==t)]-10-[t]-| ==t -
Grammar of Operators (from Language Reference) operator → operator-head operator-characters
opt operator → dot-operator-head dot-operator-characters opt operator-head → / | = | - | + | ! | * | % | < | > | & | | | ^ | ~ | ? operator-head → U+00A1–U+00A7 operator-head → U+00A9 or U+00AB operator-head → U+00AC or U+00AE operator-head → U+00B0–U+00B1, U+00B6, U+00BB, U+00BF, U+00D7, or U+00F7 operator-head → U+2016–U+2017 or U+2020–U+2027 ɿ # $ @ : , ͳͲ͑ͳ͍ -
mattt/Euler Swift Custom Operators for Mathematical Notation infix operator ∈
{ associativity left } func ∈ <T: Equatable> (left: T, right: [T]) -> Bool { return contains(right, left) } print(2 ∈ [2,3,4]) // true print(1 ∈ [2,3,4]) // false -
Build Configurations OSͷҧ͍Λٵऩ #if os(iOS) import UIKit public typealias YAVView
= UIView #else import AppKit public typealias YAVView = NSView #endif extension YAVView { private func yav_setTranslatesAutoresizingMaskIntoConstraints(flag: Bool) { #if os(iOS) setTranslatesAutoresizingMaskIntoConstraints(flag) #else translatesAutoresizingMaskIntoConstraints = flag #endif } } -
OTHER_SWIFT_FLAGS="-DCOCOAPODS" • CocoaPodsͰϞδϡʔϧ໊͕ৗʹFoo • ඇCocoaPodsͰɺMacڥͷΈFooOSXʹͳΔ #if os(iOS) || COCOAPODS import
Foo #else import FooOSX #endif -
·ͱΊ • Auto LayoutϥΠϒϥϦyavflͰɺ • Trailing ClosureͷதͰɺ • ArrayLiteralConvertibleͳͲͰదͳenumʹม͑ͭͭɺ •
Custom OperatorͰͦΕΒΛྻʹ·ͱΊ͔ͯΒɺ • ͦΕΛจࣈྻʹมͯ͠NSLayoutConstraint. constraintsWithVisualFormatΛͬͯ·͢ɻ -
Visual Format Language Ͱॻ͚ͳ͍ͷ͋Δ • ΞεϖΫτൺͷࢦఆͳͲ imageView.width = 2 *
imageView.height “Auto Layout Guide” from Apple -
ྨࣅϓϩδΣΫτ 0xc010d/VFLToolbox robb/Cartography ConstraintΛॻͨ͘ΊͷϥΠϒϥϦ matteocrippa/awesome-swift view.addConstraints(V:|-[view1]-4-[view2]-|) layout(view1, view2) { view1,
view2 in view1.width == view1.superview!.width * 0.5 view2.width == view1.width - 50 }