Code Monkey home page Code Monkey logo

autolayoutmacros's Introduction

AutoLayoutMacros

Macros for making Auto Layout more readable (and writable) without using strings.

Not using (visual format) strings means you can use variables and values directly. Works for iOS and OS X.

Examples

[view addConstraints:@[
    ALIGN_LEFT(view1, view2),
    ALIGN_TOP(view1, view2),
    PIN_WIDTH(view1, view2, TIMES(0.5))
]];

Compare

AutoLayoutMacros plain Auto Layout
ALIGN_TOP(view1, view2)
[NSLayoutConstraint constraintWithItem:view1
                             attribute:NSLayoutAttributeTop
                             relatedBy:NSLayoutRelationEqual
                                toItem:view2
                             attribute:NSLayoutAttributeTop
                            multiplier:1.0
                              constant:0.0]
INSET_LEFT(view1, 50)
[NSLayoutConstraint constraintWithItem:view1
                             attribute:NSLayoutAttributeLeft
                             relatedBy:NSLayoutRelationEqual
                                toItem:view1.superview
                             attribute:NSLayoutAttributeLeft
                            multiplier:1.0
                              constant:50.0]
PIN_WIDTH(view1, LE(100))
[NSLayoutConstraint constraintWithItem:view1
                             attribute:NSLayoutAttributeWidth
                             relatedBy:NSLayoutRelationLessThanOrEqual
                                toItem:nil
                             attribute:NSLayoutAttributeNotAnAttribute
                            multiplier:1.0
                              constant:100.0]
PIN_HEIGHT(view1, view2, TIMES(2.0))
[NSLayoutConstraint constraintWithItem:view1
                             attribute:NSLayoutAttributeHeight
                             relatedBy:NSLayoutRelationEqual
                                toItem:nil
                             attribute:NSLayoutAttributeHeight
                            multiplier:2.0
                              constant:0.0]
HSPACE(view1, view2, 50, PRIO(500))
NSLayoutConstraint *constraint =
    [NSLayoutConstraint constraintWithItem:view2
                                 attribute:NSLayoutAttributeLeft
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:view1
                                 attribute:NSLayoutAttributeRight
                                multiplier:1.0
                                  constant:0.0]
constraint.priority = 500;

Combinations

ALIGN_LEADING(view1, view2)
ALIGN_TRAILING(view1, view2)
ALIGN_LEFT(view1, view2)
ALIGN_RIGHT(view1, view2)
ALIGN_CENTER_X(view1, view2)
ALIGN_TOP(view1, view2)
ALIGN_BOTTOM(view1, view2)
ALIGN_CENTER_Y(view1, view2)
ALIGN_BASELINE(view1, view2)

ALIGN_LEADING(view1, view2, 20)
ALIGN_LEADING(view1, GE(view2))
ALIGN_LEADING(view1, LE(view2, 10))

INSET_LEADING(view1)
INSET_TRAILING(view1)
INSET_LEFT(view1)
INSET_RIGHT(view1)
INSET_CENTER_X(view1)
INSET_TOP(view1)
INSET_BOTTOM(view1)
INSET_CENTER_Y(view1)
INSET_BASELINE(view1)

INSET_LEADING(view1, -20)
INSET_LEADING(view1, GE(50))

PIN_WIDTH(view1, view2)
PIN_HEIGHT(view1, view2)

PIN_WIDTH(view1, view2, 20)
PIN_WIDTH(view1, view2, TIMES(2))
PIN_WIDTH(view1, view2, TIMES(2), 10)
PIN_WIDTH(view1, EQ(view2, TIMES(2), 10))
PIN_WIDTH(view1, LE(view2, 50), PRIO(500))

PIN_WIDTH(view1, 100)
ALIGN_HEIGHT(view1, 100)

PIN_WIDTH(view1, GE(100))
PIN_WIDTH(view1, 100, LOW)

HSPACE(view1, view2)
VSPACE(view1, view2)

HSPACE(view1, view2, 10)
HSPACE(view1, view2, GE(10))
HSPACE(view1, view2, LE(10), HIGH)
HSPACE(view1, view2, 10, REQUIRED)

Prefix

All macros use the ALM_ prefix by default, e.g. ALM_ALIGN_LEFT if you import AutoLayoutMacros.h. The shorter macros above are available in AutoLayoutMacrosShort.h.

Don't use functions that start with _ALM. These are for internal use.

Setup

CocoaPods

Add the following to your Podfile:

pod 'AutoLayoutMacros'

Then run pod install as usual and #import <AutoLayoutMacros/AutoLayoutMacros.h> or #import <AutoLayoutMacros/AutoLayoutMacrosShort.h>.

Manual Installation

If you don't use CocoaPods, copy the AutoLayoutMacros directory from the repository into your project.

autolayoutmacros's People

Contributors

nschum avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  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.