-
Notifications
You must be signed in to change notification settings - Fork 0
/
BDKGeometry.m
92 lines (71 loc) · 2.95 KB
/
BDKGeometry.m
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
/*
* BDKGeometry.m
* Written by Ben Kreeger.
*/
CGRect CGRectSetSize(CGRect rect, CGSize size) {
rect.size = size;
return rect;
}
CGRect CGRectAdjustSize(CGRect rect, CGSize size) { return CGRectSetSize(rect, size); }
CGRect CGRectSetOrigin(CGRect rect, CGPoint origin) {
rect.origin = origin;
return CGRectIntegral(rect);
}
CGRect CGRectAdjustOrigin(CGRect rect, CGPoint origin) { return CGRectSetOrigin(rect, origin); }
CGRect CGRectSetWidth(CGRect rect, CGFloat width) {
rect.size.width = width;
return rect;
}
CGRect CGRectAdjustWidth(CGRect rect, CGFloat width) { return CGRectSetWidth(rect, width); }
CGRect CGRectSetHeight(CGRect rect, CGFloat height) {
rect.size.height = height;
return rect;
}
CGRect CGRectAdjustHeight(CGRect rect, CGFloat height) { return CGRectSetHeight(rect, height); }
CGRect CGRectSetYOrigin(CGRect rect, CGFloat yOrigin) {
rect.origin.y = yOrigin;
return CGRectIntegral(rect);
}
CGRect CGRectAdjustYOrigin(CGRect rect, CGFloat yOrigin) { return CGRectSetYOrigin(rect, yOrigin); }
CGRect CGRectSetXOrigin(CGRect rect, CGFloat xOrigin) {
rect.origin.x = xOrigin;
return CGRectIntegral(rect);
}
CGRect CGRectAdjustXOrigin(CGRect rect, CGFloat xOrigin) { return CGRectSetXOrigin(rect, xOrigin); }
CGRect CGRectCenterRectInRect(CGRect subRect, CGRect masterRect) {
CGPoint origin = CGPointMake((masterRect.size.width - subRect.size.width) / 2.0f,
(masterRect.size.height - subRect.size.height) / 2.0f);
return CGRectIntegral(CGRectSetOrigin(subRect, origin));
}
CGRect CGRectCenterRectInRectHorizontally(CGRect subRect, CGRect masterRect) {
CGPoint origin = CGPointMake((CGRectGetWidth(masterRect) - CGRectGetWidth(subRect)) / 2.0f, subRect.origin.y);
return CGRectIntegral(CGRectSetOrigin(subRect, origin));
}
CGRect CGRectCenterRectInRectVertically(CGRect subRect, CGRect masterRect) {
CGPoint origin = CGPointMake(subRect.origin.x, (CGRectGetHeight(masterRect) - CGRectGetHeight(subRect)) / 2.0f);
return CGRectIntegral(CGRectSetOrigin(subRect, origin));
}
CGRect CGRectInsetByPercent(CGRect rect, CGFloat xPercent, CGFloat yPercent) {
CGRect oldRect = rect;
rect = CGRectSetSize(rect, CGSizeMake(rect.size.width * xPercent, rect.size.height * yPercent));
return CGRectCenterRectInRect(rect, oldRect);
}
CGRect CGRectSubtract(CGRect rect, CGFloat amount, CGRectEdge edge) {
CGRect useless = CGRectNull;
CGRect newRect = CGRectNull;
CGRectDivide(rect, &useless, &newRect, amount, edge);
return CGRectIntegral(newRect);
}
void CGRectIntegralSizeToFit(UIView *view) {
[view sizeToFit];
view.frame = CGRectIntegral(view.frame);
}
CGSize CGSizeByDoubling(CGSize size) {
return CGSizeMake(size.width * 2.0f, size.height * 2.0f);
}
CGRect CGRectByDoublingSize(CGRect rect) {
return CGRectSetSize(rect, CGSizeByDoubling(rect.size));
}
CGRect CGRectFor1PxStroke(CGRect rect) {
return CGRectInset(rect, 0.5, 0.5);
}