FunctionalLayout 1.3.0

FunctionalLayout 1.3.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release May 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Adrian Zubarev.



FunctionalLayout


Write beautiful constraints:

import FunctionalLayout

let constraint = Constraint.where(view1.centerXAnchor == view2.centerXAnchor)
    .named("CenterXConstraint")
    .withPriority(of: 800)
    .shouldBeArchived()
    .nsLayoutConstraint

constraint.isActive = true

self.constraint = Constraint.where(view1.centerYAnchor == view2.centerYAnchor)
    .named("CenterYConstraint")
    .activated()
    .nsLayoutConstraint

NSLayoutConstraint.activate([
    Constraint.where(view1.widthAnchor == 200).named("WidthConstraint"),
    Constraint.where(view1.heightAnchor == view2.heightAnchor)
        .multiplied(with: 2.0)
        .offset(by: -50)
        .named("HeightConstraint")
        .withPriority(of: 1000)
        .shouldBeArchived()
        .assignedTo(&self.someConstraintVariable)
])

Installation

FunctionalLayout is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'FunctionalLayout'

Explanation

This API contains a complex function permutation graph modeled with protocols. Each letter represents a function that the coresponding protocol contains.

Function permutation protocol graph

Red nodes represent the starting points for the four static functions:

Constraint.where(_: Layout<NSLayoutXAxisAnchor>) -> OWSN
Constraint.where(_: Layout<NSLayoutYAxisAnchor>) -> OWSN
Constraint.where(_: Layout<NSLayoutDimension>) -> OMWSN
Constraint.where(_: Layout<CGFloat>) -> WSN

An instance of Layout<...> can be constructed with three different infix operators.

func ==<T>(lhs: NSLayoutAnchor<T>, rhs: NSLayoutAnchor<T>) -> Layout<T>
func >=<T>(lhs: NSLayoutAnchor<T>, rhs: NSLayoutAnchor<T>) -> Layout<T>
func <=<T>(lhs: NSLayoutAnchor<T>, rhs: NSLayoutAnchor<T>) -> Layout<T>

func ==(lhs: NSLayoutDimension, rhs: CGFloat) -> Layout<CGFloat>
func >=(lhs: NSLayoutDimension, rhs: CGFloat) -> Layout<CGFloat>
func <=(lhs: NSLayoutDimension, rhs: CGFloat) -> Layout<CGFloat>

This API guarantees value semantics [1] for all types except for ActivationInterface and NSLayoutConstraintInterface. For all guaranteed value semantics types, each chain creates a new immutable value type instance under the hood.

[1] Value semantics: realm.io/news/swift-gallagher-value-semantics

Todo

  • [x] Create clean API.
  • [ ] Test Carthage compatibility.
  • [ ] Link to CodeCov and Travis CI.
  • [ ] Write docs.
  • [ ] Write tests.

License

FunctionalLayout is available under the MIT license. See the LICENSE file for more info.