MASConstraint.h 4.12 KB
//
//  MASConstraint.h
//  Masonry
//
//  Created by Jonas Budelmann on 22/07/13.
//  Copyright (c) 2013 cloudling. All rights reserved.
//

#import <UIKit/UIKit.h>

enum {
    MASLayoutPriorityRequired = UILayoutPriorityRequired,
    MASLayoutPriorityDefaultHigh = UILayoutPriorityDefaultHigh,
    MASLayoutPriorityDefaultMedium = 500,
    MASLayoutPriorityDefaultLow = UILayoutPriorityDefaultLow,
    MASLayoutPriorityFittingSizeLevel = UILayoutPriorityFittingSizeLevel,
};
typedef float MASLayoutPriority;

/**
 *	Enables Constraints to be created with chainable syntax
 *  Constraint can represent single NSLayoutConstraint (MASViewConstraint) 
 *  or a group of NSLayoutConstraints (MASComposisteConstraint)
 */
@protocol MASConstraint <NSObject>

/**
 *	Modifies the NSLayoutConstraint constant,
 *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 
 *  NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight
 */
@property (nonatomic, copy, readonly) id<MASConstraint> (^insets)(UIEdgeInsets insets);

/**
 *	Modifies the NSLayoutConstraint constant,
 *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
 *  NSLayoutAttributeWidth, NSLayoutAttributeHeight
 */
@property (nonatomic, copy, readonly) id<MASConstraint> (^sizeOffset)(CGSize offset);

/**
 *	Modifies the NSLayoutConstraint constant,
 *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
 *  NSLayoutAttributeCenterX, NSLayoutAttributeCenterY
 */
@property (nonatomic, copy, readonly) id<MASConstraint> (^centerOffset)(CGPoint offset);


/**
 *	Modifies the NSLayoutConstraint constant
 */
@property (nonatomic, copy, readonly) id<MASConstraint> (^offset)(CGFloat offset);

/**
 *	Sets the NSLayoutConstraint multiplier property
 */
@property (nonatomic, copy, readonly) id<MASConstraint> (^percent)(CGFloat percent);

/**
 *	Sets the NSLayoutConstraint priority to a float or MASLayoutPriority
 */
@property (nonatomic, copy, readonly) id<MASConstraint> (^priority)(MASLayoutPriority priority);

/**
 *	Sets the NSLayoutConstraint priority to MASLayoutPriorityLow
 */
@property (nonatomic, copy, readonly) id<MASConstraint> (^priorityLow)();

/**
 *	Sets the NSLayoutConstraint priority to MASLayoutPriorityMedium
 */
@property (nonatomic, copy, readonly) id<MASConstraint> (^priorityMedium)();

/**
 *	Sets the NSLayoutConstraint priority to MASLayoutPriorityHigh
 */
@property (nonatomic, copy, readonly) id<MASConstraint> (^priorityHigh)();

/**
 *	Sets the constraint relation to NSLayoutRelationEqual
 *  returns a block which accepts one of the following:
 *    MASViewAttribute, UIView, NSNumber, NSArray
 *  see readme for more details.
 */
@property (nonatomic, copy, readonly) id<MASConstraint> (^equalTo)(id attr);

/**
 *	Sets the constraint relation to NSLayoutRelationGreaterThanOrEqual
 *  returns a block which accepts one of the following:
 *    MASViewAttribute, UIView, NSNumber, NSArray
 *  see readme for more details.
 */
@property (nonatomic, copy, readonly) id<MASConstraint> (^greaterThanOrEqualTo)(id attr);

/**
 *	Sets the constraint relation to NSLayoutRelationLessThanOrEqual
 *  returns a block which accepts one of the following:
 *    MASViewAttribute, UIView, NSNumber, NSArray
 *  see readme for more details.
 */
@property (nonatomic, copy, readonly) id<MASConstraint> (^lessThanOrEqualTo)(id attr);

/**
 *	optional semantic property which has no effect but improves the readability of constraint
 */
@property (nonatomic, copy, readonly) id<MASConstraint> with;

/**
 *	Sets the constraint debug name
 */
@property (nonatomic, copy, readonly) id<MASConstraint> (^key)(id key);

/**
 *	Creates a NSLayoutConstraint. The constraint is added to the first view or the or the closest common superview of the first and second view. 
 */
- (void)commit;

@end

@protocol MASConstraintDelegate <NSObject>

/**
 *	Notifies the delegate when the constraint is has the minimum set of properties.
 *
 *	@param	constraint	a constraint that has at least a NSLayoutRelation and view
 */
- (void)addConstraint:(id<MASConstraint>)constraint;

@end