MASConstraintBuilder.m
3.22 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
//
// MASConstraintBuilder.m
// Masonry
//
// Created by Jonas Budelmann on 20/07/13.
// Copyright (c) 2013 cloudling. All rights reserved.
//
#import "MASConstraintBuilder.h"
#import "MASViewConstraint.h"
#import "MASCompositeConstraint.h"
#import "MASViewAttribute.h"
@interface MASConstraintBuilder () <MASConstraintDelegate>
@property (nonatomic, strong) UIView *view;
@property (nonatomic, strong) NSMutableArray *constraints;
@end
@implementation MASConstraintBuilder
- (id)initWithView:(UIView *)view {
self = [super init];
if (!self) return nil;
self.view = view;
self.constraints = NSMutableArray.new;
return self;
}
- (void)commit {
for (id<MASConstraint> constraint in self.constraints) {
[constraint commit];
}
[self.constraints removeAllObjects];
}
#pragma mark - MASConstraintDelegate
- (void)addConstraint:(id<MASConstraint>)constraint {
[self.constraints addObject:constraint];
}
#pragma mark - constraint properties
- (id<MASConstraint>)constraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
MASViewAttribute *viewAttribute = [[MASViewAttribute alloc] initWithView:self.view layoutAttribute:layoutAttribute];
MASViewConstraint *constraint = [[MASViewConstraint alloc] initWithFirstViewAttribute:viewAttribute];
constraint.delegate = self;
return constraint;
}
#pragma mark - standard Attributes
- (id<MASConstraint>)left {
return [self constraintWithLayoutAttribute:NSLayoutAttributeLeft];
}
- (id<MASConstraint>)top {
return [self constraintWithLayoutAttribute:NSLayoutAttributeTop];
}
- (id<MASConstraint>)right {
return [self constraintWithLayoutAttribute:NSLayoutAttributeRight];
}
- (id<MASConstraint>)bottom {
return [self constraintWithLayoutAttribute:NSLayoutAttributeBottom];
}
- (id<MASConstraint>)leading {
return [self constraintWithLayoutAttribute:NSLayoutAttributeLeading];
}
- (id<MASConstraint>)trailing {
return [self constraintWithLayoutAttribute:NSLayoutAttributeTrailing];
}
- (id<MASConstraint>)width {
return [self constraintWithLayoutAttribute:NSLayoutAttributeWidth];
}
- (id<MASConstraint>)height {
return [self constraintWithLayoutAttribute:NSLayoutAttributeHeight];
}
- (id<MASConstraint>)centerX {
return [self constraintWithLayoutAttribute:NSLayoutAttributeCenterX];
}
- (id<MASConstraint>)centerY {
return [self constraintWithLayoutAttribute:NSLayoutAttributeCenterY];
}
- (id<MASConstraint>)baseline {
return [self constraintWithLayoutAttribute:NSLayoutAttributeBaseline];
}
#pragma mark - composite Attributes
- (id<MASConstraint>)edges {
MASCompositeConstraint *constraint = [[MASCompositeConstraint alloc] initWithView:self.view type:MASCompositeViewConstraintTypeEdges];
constraint.delegate = self;
return constraint;
}
- (id<MASConstraint>)size {
MASCompositeConstraint *constraint = [[MASCompositeConstraint alloc] initWithView:self.view type:MASCompositeViewConstraintTypeSize];
constraint.delegate = self;
return constraint;
}
- (id<MASConstraint>)center {
MASCompositeConstraint *constraint = [[MASCompositeConstraint alloc] initWithView:self.view type:MASCompositeViewConstraintTypeCenter];
constraint.delegate = self;
return constraint;
}
@end