Giter Site home page Giter Site logo

cartography's Introduction

Cartography

Set up your Auto Layout constraints declaratively and without any stringly typing!

How to use

Call the layout function with your UIView or NSView instances as well as a closure in which you declare the constraints between the different attributes of your views:

layout(view1, view2) { view1, view2 in
    view1.width   == (view1.superview!.width - 50) * 0.5
    view2.width   == view1.width - 50
    view1.height  == 40
    view2.height  == view1.height
    view1.centerX == view1.superview!.centerX
    view2.centerX == view1.centerX

    view1.top >= view1.superview!.top + 20
    view2.top == view1.bottom + 20
}

Supported attributes

Cartography supports all built-in attributes as of iOS 7 and OS X 10.9, those are: width, height, top, right bottom, left, leading, trailing, centerX, centerY and baseline. These can be further refined using the following operators: *, /, + and -.

Additionally, it supports convenient compound attributes that allow you to assign multiple attributes at once:

layout(view) { view in
    view.size   == view.superview!.size / 2
    view.center == view.superview!.center
}
layout(view) { view in
    view.edges == inset(view.superview!.edges, 20, 20, 40, 20); return
}

(We need to return explicitly here to work around a bug in Swift, this only affects single line blocks, however.)

Setting priorities

You can set the priorities of your constraints using the ~ operator:

layout(view) { view in
    view.width  >= 200 ~ 100
    view.height >= 200 ~ 100
}

Capturing constraints

Since the ==, >=, <= and ~ emit NSLayoutConstraint instances, you can capture their results if you need to refer to the layout constraints at a later time:

var width: NSLayoutConstraint?

layout(view) { view in
    width = (view.width == 200 ~ 100); return
}

Note that declaring compound attributes returns multiple constraints at once:

var constraints: NSLayoutConstraint[]?

layout(view) { view in
    constraints = (view.size == view.superview!.size ~ 100); return
}

About Cartography

Cartography is written in Swift so it's pretty much beta by definition. It was inspired by the excellent FLKAutoLayout by Florian Kugler.

cartography's People

Contributors

robb avatar jamescmartinez avatar nschum avatar

Watchers

Florian Krüger avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.