Giter VIP home page Giter VIP logo

masonrygenerator's Introduction

Masonry Generator

A Mansory extension to generate native NSLayoutConstraint code from Mansory code.

Why?

Sometime I want to write constraints code easily but I dont want to add another dependency (such as writing a library). Generating native constraints can be handy.

Usage

Import MansoryGenerator:

#import "MasonryGenerator.h"

Then add following line before your layout code:

[MasonryGenerator install];

Now any mas_makeConstraints: or mas_updateConstraints: calls will print the native constraints code on console.

UIView *superview = self.view;
superview.mas_key = @"superview";

UIView *view1 = UIView.new;
view1.mas_key = @"view1";
view1.backgroundColor = UIColor.greenColor;
view1.layer.borderColor = UIColor.blackColor.CGColor;
view1.layer.borderWidth = 2;
[superview addSubview:view1];

UIView *view2 = UIView.new;
view2.mas_key = @"view2";
view2.backgroundColor = UIColor.redColor;
view2.layer.borderColor = UIColor.blackColor.CGColor;
view2.layer.borderWidth = 2;
[superview addSubview:view2];

UIView *view3 = UIView.new;
view3.mas_key = @"view3";

[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.greaterThanOrEqualTo(superview.mas_top).offset(padding + 20);
    make.left.equalTo(superview.mas_left).offset(padding);
    make.bottom.equalTo(view3.mas_top).offset(-padding);
    make.right.equalTo(view2.mas_left).offset(-padding);
    make.width.equalTo(view2.mas_width);
    
    make.height.equalTo(view2.mas_height);
    make.height.equalTo(view3.mas_height);
}];

Following code will be generated on console:

=====================================================
[superview addConstraint:[NSLayoutConstraint constraintWithItem:view1
                                                      attribute:NSLayoutAttributeTop
                                                      relatedBy:NSLayoutRelationGreaterThanOrEqual
                                                         toItem:superview
                                                      attribute:NSLayoutAttributeTop
                                                     multiplier:1.0
                                                       constant:30.0]];

[superview addConstraint:[NSLayoutConstraint constraintWithItem:view1
                                                      attribute:NSLayoutAttributeLeft
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:superview
                                                      attribute:NSLayoutAttributeLeft
                                                     multiplier:1.0
                                                       constant:10.0]];

[superview addConstraint:[NSLayoutConstraint constraintWithItem:view1
                                                      attribute:NSLayoutAttributeBottom
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:view3
                                                      attribute:NSLayoutAttributeTop
                                                     multiplier:1.0
                                                       constant:-10.0]];

[superview addConstraint:[NSLayoutConstraint constraintWithItem:view1
                                                      attribute:NSLayoutAttributeRight
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:view2
                                                      attribute:NSLayoutAttributeLeft
                                                     multiplier:1.0
                                                       constant:-10.0]];

[superview addConstraint:[NSLayoutConstraint constraintWithItem:view1
                                                      attribute:NSLayoutAttributeWidth
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:view2
                                                      attribute:NSLayoutAttributeWidth
                                                     multiplier:1.0
                                                       constant:0.0]];

[superview addConstraint:[NSLayoutConstraint constraintWithItem:view1
                                                      attribute:NSLayoutAttributeHeight
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:view2
                                                      attribute:NSLayoutAttributeHeight
                                                     multiplier:1.0
                                                       constant:0.0]];

[superview addConstraint:[NSLayoutConstraint constraintWithItem:view1
                                                      attribute:NSLayoutAttributeHeight
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:view3
                                                      attribute:NSLayoutAttributeHeight
                                                     multiplier:1.0
                                                       constant:0.0]];
=====================================================

Masonry Generator find the views name by mas_key field, so always set the mas_key to the name of the view.

Important: You should only include MansoryGenerator during development!

License

MIT License.

masonrygenerator's People

Contributors

siuying avatar

Stargazers

 avatar Jiyee Sheng avatar wcnjeusr avatar Juan Carlos Christensen avatar  avatar

Watchers

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