-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNSLayoutConstraint+Equations.m
More file actions
194 lines (156 loc) · 7.24 KB
/
NSLayoutConstraint+Equations.m
File metadata and controls
194 lines (156 loc) · 7.24 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//
// NSLayoutConstraint+Equations.m
// Rubrik
//
// Created by Morgan Harris on 24/01/13.
// Copyright (c) 2013 Morgan Harris. All rights reserved.
//
#import "NSLayoutConstraint+Equations.h"
#define PROPERTY "[a-zA-Z_][a-zA-Z0-9_]*"
#define WHITESPACE "\\s*"
#define RELATION "=|<|>"
#define NUMBER "\\d+(?:\\.\\d+)?"
@implementation NSLayoutConstraint (Equations)
+ (NSLayoutConstraint*) constraintWithFormula:(NSString *)formula LHS:(id)lhs RHS:(id)rhs
{
//parse the formula
//the format is property { = | < | > } [multiplier *] property [{ + | - } constant]
//or if RHS is nil property { = | < | > } constant
NSString *lhsPropertyString, *rhsPropertyString, *relationString;
CGFloat multiplier = 1.0, constant = 0.0;
static NSRegularExpression* expr = nil;
static NSRegularExpression* constExpr = nil;
static NSDictionary* layoutDict;
static NSDictionary* relationDict;
static dispatch_once_t onceToken;
static int indexLHS = 1;
static int indexRelation = 2;
static int indexMultiplier = 3;
static int indexConstant_noRHS = 3; // Used to index unary view expressions
static int indexRHS = 4;
static int indexOp = 5;
static int indexConstant = 6;
dispatch_once(&onceToken, ^{
NSString* pattern = @"(" PROPERTY ")" WHITESPACE "(" RELATION ")" WHITESPACE "(?:(" NUMBER ")" WHITESPACE "\\*)?" WHITESPACE "(" PROPERTY ")" WHITESPACE "(?:([\\+-])" WHITESPACE "(" NUMBER "))?";
NSError* err;
expr = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&err];
if (expr == nil) {
NSLog(@"%@",err);
abort();
}
pattern = @"(" PROPERTY ")" WHITESPACE "(" RELATION ")" WHITESPACE "(" NUMBER ")";
constExpr = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&err];
if (constExpr == nil) {
NSLog(@"%@",err);
abort();
}
layoutDict = @{
@"baseline" : @(NSLayoutAttributeBaseline),
@"bottom" : @(NSLayoutAttributeBottom),
@"centerX" : @(NSLayoutAttributeCenterX),
@"centerY" : @(NSLayoutAttributeCenterY),
@"height" : @(NSLayoutAttributeHeight),
@"leading" : @(NSLayoutAttributeLeading),
@"left" : @(NSLayoutAttributeLeft),
@"right" : @(NSLayoutAttributeRight),
@"top" : @(NSLayoutAttributeTop),
@"trailing" : @(NSLayoutAttributeTrailing),
@"width" : @(NSLayoutAttributeWidth)
};
relationDict = @{
@"=" : @(NSLayoutRelationEqual),
@"<" : @(NSLayoutRelationLessThanOrEqual),
@">" : @(NSLayoutRelationGreaterThanOrEqual)
};
});
if (rhs == nil) {
NSTextCheckingResult* rslt = [constExpr firstMatchInString:formula options:0 range:NSMakeRange(0, formula.length)];
lhsPropertyString = [formula substringWithRange:[rslt rangeAtIndex:indexLHS]];
relationString = [formula substringWithRange:[rslt rangeAtIndex:indexRelation]];
constant = [[formula substringWithRange:[rslt rangeAtIndex:indexConstant_noRHS]] floatValue];
NSLayoutAttribute lhsAttribute = [layoutDict[lhsPropertyString] integerValue];
NSLayoutRelation relation = [relationDict[relationString] integerValue];
return [self constraintWithItem:lhs
attribute:lhsAttribute
relatedBy:relation
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:constant];
} else {
NSTextCheckingResult* rslt = [expr firstMatchInString:formula options:0 range:NSMakeRange(0, formula.length)];
//assign our strings
lhsPropertyString = [formula substringWithRange:[rslt rangeAtIndex:indexLHS]];
relationString = [formula substringWithRange:[rslt rangeAtIndex:indexRelation]];
if ([rslt rangeAtIndex:indexMultiplier].length > 0) {
multiplier = [[formula substringWithRange:[rslt rangeAtIndex:indexMultiplier]] floatValue];
}
rhsPropertyString = [formula substringWithRange:[rslt rangeAtIndex:indexRHS]];
if ([rslt rangeAtIndex:indexConstant].length > 0) {
NSString *op = [formula substringWithRange:[rslt rangeAtIndex:indexOp]];
constant = [[formula substringWithRange:[rslt rangeAtIndex:indexConstant]] floatValue];
if ([op isEqual:@"-"]) {
constant *= -1;
}
}
//translate property strings to properties
NSLayoutAttribute lhsAttribute = [layoutDict[lhsPropertyString] integerValue];
NSLayoutAttribute rhsAttribute = [layoutDict[rhsPropertyString] integerValue];
NSLayoutRelation relation = [relationDict[relationString] integerValue];
return [self constraintWithItem:lhs
attribute:lhsAttribute
relatedBy:relation
toItem:rhs
attribute:rhsAttribute
multiplier:multiplier
constant:constant];
}
}
@end
@implementation UIView (Equations)
- (NSLayoutConstraint *)buildConstraint:(NSString *)formula with:(UIView *)otherView priority:(UILayoutPriority)priority
{
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithFormula:formula LHS:self RHS:otherView];
constraint.priority = priority;
return constraint;
}
- (NSLayoutConstraint *)buildConstraint:(NSString *)formula with:(UIView *)otherView
{
// Build constraints a bit more naturally
return [self buildConstraint:formula with:otherView priority:UILayoutPriorityRequired];
}
- (NSLayoutConstraint *)constrain:(NSString *)formula to:(UIView *)otherView
{
UIView* commonSuperview = nil;
if (otherView != nil) {
// The first thing we do is find the closest common ancestor.
// We do this by adding all the ancestors, one by one, to two sets
// As soon as they have a common object, we've got the closest
// common ancestor.
NSMutableSet * s1, * s2;
s1 = [NSMutableSet setWithObject:self];
s2 = [NSMutableSet setWithObject:otherView];
UIView* v1 = self, *v2 = otherView;
do {
v1 = v1.superview;
v2 = v2.superview;
if (v1)
[s1 addObject:v1];
if (v2)
[s2 addObject:v2];
if ([s1 intersectsSet:s2]) {
[s1 intersectSet:s2];
commonSuperview = [s1 anyObject];
break;
}
} while (v1 && v2);
NSAssert(commonSuperview != nil, @"no common superview");
} else {
commonSuperview = self.superview;
}
//Now we've got the closest common ancestor, we just make the constraint and add it
NSLayoutConstraint* constraint = [self buildConstraint:formula with:otherView];
[commonSuperview addConstraint:constraint];
return constraint;
}
@end