forked from Seitk/erica-constraints
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEricaConstraints.h
More file actions
executable file
·108 lines (81 loc) · 3.44 KB
/
EricaConstraints.h
File metadata and controls
executable file
·108 lines (81 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
Erica Sadun, http://ericasadun.com
Constraints
*/
// Constraint Installation
#import "ConstraintUtilities-Install.h"
// Prebuilt Functions, Macros, Etc
#import "ConstraintUtilities-Layout.h"
#import "Constraints-CreationMacros.h"
// Constraint Matching, Retrieval
#import "NSObject-Nametag.h"
#import "ConstraintUtilities-Matching.h"
// Constraint Description for Debugging
#import "NSObject-Description.h"
#import "ConstraintUtilities-Description.h"
/*
*
*
CONVENIENCE
*
*
*/
#pragma mark - Utility
/*
*
*
TESTING CONSTRAINT ELEMENTS
*
*
*/
#define IS_VALID_RELATION(RELATION) [@[@(NSLayoutFormatAlignAllTop), @(NSLayoutRelationEqual), @(NSLayoutRelationGreaterThanOrEqual)] containsObject:@(RELATION)]
#define IS_VALID_ATTRIBUTE(ATTRIBUTE) [@[@(NSLayoutAttributeWidth), @(NSLayoutAttributeHeight), @(NSLayoutAttributeCenterX), @(NSLayoutAttributeCenterY), @(NSLayoutAttributeLeft), @(NSLayoutAttributeRight), @(NSLayoutAttributeTop), @(NSLayoutAttributeBottom), @(NSLayoutAttributeLeading), @(NSLayoutAttributeTrailing), @(NSLayoutAttributeBaseline)] containsObject:@(ATTRIBUTE)]
/*
*
*
BASIC FORMAT-BASED CONSTRAINTS
*
*
*/
#pragma mark - Simple Visual Constraints
#define CONSTRAINTS(FORMAT, ...) ^NSArray *(){NSDictionary *_bindings = NSDictionaryOfVariableBindings(__VA_ARGS__); return [NSLayoutConstraint constraintsWithVisualFormat:(FORMAT) options:0 metrics:nil views:_bindings];}()
#define CONSTRAINTS_WITH_OPTIONS(FORMAT, OPTIONS, ...) ^NSArray *(){NSDictionary *_bindings = NSDictionaryOfVariableBindings(__VA_ARGS__); return [NSLayoutConstraint constraintsWithVisualFormat:(FORMAT) options:OPTIONS metrics:nil views:_bindings];}()
/*
*
*
INSTALLING CONSTRAINTS. Ugly. Sorry.
*
*
*/
#pragma mark - Installing Visual Constraints
// Apply format-based constraints
#define CONSTRAIN_VIEWS(PRIORITY, NAME, FORMAT, ...) \
{\
NSArray *_tmpArray = CONSTRAINTS(FORMAT, __VA_ARGS__); \
for (NSLayoutConstraint *_tmpConstraint in _tmpArray) \
{ \
_tmpConstraint.nametag = NAME; \
[_tmpConstraint install:PRIORITY]; \
} \
}
// Common constraint install entry point using no naming and default layout priority
#define DEFAULT_LAYOUT_PRIORITY LayoutPriorityRequired
#define CONSTRAIN(FORMAT, ...) CONSTRAIN_VIEWS(DEFAULT_LAYOUT_PRIORITY, nil, FORMAT, __VA_ARGS__)
#pragma mark - Installing single and array constraints
// Install a single constraint
#define _INSTALL_CONSTRAINT(PRIORITY, NAME, CONSTRAINT) {[CONSTRAINT setNametag:NAME]; [CONSTRAINT install:PRIORITY];}
// Install an array of constraints
#define _INSTALL_CONSTRAINT_ARRAY(PRIORITY, NAME, ARRAY) {for (NSLayoutConstraint *_tmpConstraint in ARRAY) {_INSTALL_CONSTRAINT(PRIORITY, NAME, _tmpConstraint);}}
// Install a list of mixed single and array constraints
#define _INSTALL_CONSTRAINTS(PRIORITY, NAME, ...) {\
NSArray *_tmpArray = [NSArray arrayWithObjects: __VA_ARGS__]; \
for (NSObject *_eachItem in _tmpArray){ \
if ([(_eachItem) isKindOfClass:[NSArray class]]) \
{_INSTALL_CONSTRAINT_ARRAY(PRIORITY, NAME, (NSArray *) _eachItem);} \
else \
{_INSTALL_CONSTRAINT(PRIORITY, NAME, (NSLayoutConstraint *) _eachItem);}\
}\
}
#define REMOVE_CONSTRAINTS(ARRAY) do {for (NSLayoutConstraint *_tmpConstraint in ARRAY) [_tmpConstraint remove];} while (0)
// Convenience entry point to avoid forcing semaphore at the end
#define INSTALL_CONSTRAINTS(PRIORITY, NAME, ...) _INSTALL_CONSTRAINTS(PRIORITY, NAME, __VA_ARGS__, nil)