This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
TTRoundedRectangleShape.m
179 lines (136 loc) · 6.99 KB
/
TTRoundedRectangleShape.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
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
//
// Copyright 2009-2011 Facebook
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "Three20Style/TTRoundedRectangleShape.h"
// Style (private)
#import "Three20Style/private/TTShapeInternal.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation TTRoundedRectangleShape
@synthesize topLeftRadius = _topLeftRadius;
@synthesize topRightRadius = _topRightRadius;
@synthesize bottomRightRadius = _bottomRightRadius;
@synthesize bottomLeftRadius = _bottomLeftRadius;
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Class public
///////////////////////////////////////////////////////////////////////////////////////////////////
+ (TTRoundedRectangleShape*)shapeWithRadius:(CGFloat)radius {
TTRoundedRectangleShape* shape = [[[TTRoundedRectangleShape alloc] init] autorelease];
shape.topLeftRadius = shape.topRightRadius = shape.bottomRightRadius = shape.bottomLeftRadius
= radius;
return shape;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
+ (TTRoundedRectangleShape*)shapeWithTopLeft:(CGFloat)topLeft topRight:(CGFloat)topRight
bottomRight:(CGFloat)bottomRight bottomLeft:(CGFloat)bottomLeft {
TTRoundedRectangleShape* shape = [[[TTRoundedRectangleShape alloc] init] autorelease];
shape.topLeftRadius = topLeft;
shape.topRightRadius = topRight;
shape.bottomRightRadius = bottomRight;
shape.bottomLeftRadius = bottomLeft;
return shape;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Public
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)addToPath:(CGRect)rect {
[self openPath:rect];
CGFloat fw = rect.size.width;
CGFloat fh = rect.size.height;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, fw, floor(fh/2));
CGContextAddArcToPoint(context, fw, fh, floor(fw/2), fh, RD(_bottomRightRadius));
CGContextAddArcToPoint(context, 0, fh, 0, floor(fh/2), RD(_bottomLeftRadius));
CGContextAddArcToPoint(context, 0, 0, floor(fw/2), 0, RD(_topLeftRadius));
CGContextAddArcToPoint(context, fw, 0, fw, floor(fh/2), RD(_topRightRadius));
[self closePath:rect];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)addInverseToPath:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat fw = rect.size.width;
CGFloat fh = rect.size.height;
CGFloat width = 5.0f;
CGRect shadowRect = CGRectMake(-width, -width, fw+width*2, fh+width*2);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, nil, shadowRect);
CGPathCloseSubpath(path);
CGPathMoveToPoint(path, nil, fw, floor(fh/2));
CGPathAddArcToPoint(path, nil, fw, fh, floor(fw/2), fh, RD(_bottomRightRadius));
CGPathAddArcToPoint(path, nil, 0, fh, 0, floor(fh/2), RD(_bottomLeftRadius));
CGPathAddArcToPoint(path, nil, 0, 0, floor(fw/2), 0, RD(_topLeftRadius));
CGPathAddArcToPoint(path, nil, fw, 0, fw, floor(fh/2), RD(_topRightRadius));
CGPathCloseSubpath(path);
[self openPath:rect];
CGContextAddPath(context, path);
[self closePath:rect];
CGPathRelease(path);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)addTopEdgeToPath:(CGRect)rect lightSource:(NSInteger)lightSource {
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat fw = rect.size.width;
CGFloat fh = rect.size.height;
if (lightSource >= 0 && lightSource <= 90) {
CGContextMoveToPoint(context, RD(_topLeftRadius), 0);
} else {
CGContextMoveToPoint(context, 0, RD(_topLeftRadius));
CGContextAddArcToPoint(context, 0, 0, RD(_topLeftRadius), 0, RD(_topLeftRadius));
}
CGContextAddArcToPoint(context, fw, 0, fw, RD(_topRightRadius), RD(_topRightRadius));
CGContextAddLineToPoint(context, fw, RD(_topRightRadius));
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)addRightEdgeToPath:(CGRect)rect lightSource:(NSInteger)lightSource {
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat fw = rect.size.width;
CGFloat fh = rect.size.height;
CGContextMoveToPoint(context, fw, RD(_topRightRadius));
CGContextAddArcToPoint(context, fw, fh, fw-RD(_bottomRightRadius), fh, RD(_bottomRightRadius));
CGContextAddLineToPoint(context, fw-RD(_bottomRightRadius), fh);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)addBottomEdgeToPath:(CGRect)rect lightSource:(NSInteger)lightSource {
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat fw = rect.size.width;
CGFloat fh = rect.size.height;
CGContextMoveToPoint(context, fw-RD(_bottomRightRadius), fh);
CGContextAddLineToPoint(context, RD(_bottomLeftRadius), fh);
CGContextAddArcToPoint(context, 0, fh, 0, fh-RD(_bottomLeftRadius), RD(_bottomLeftRadius));
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)addLeftEdgeToPath:(CGRect)rect lightSource:(NSInteger)lightSource {
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat fh = rect.size.height;
CGContextMoveToPoint(context, 0, fh-RD(_bottomLeftRadius));
CGContextAddLineToPoint(context, 0, RD(_topLeftRadius));
if (lightSource >= 0 && lightSource <= 90) {
CGContextAddArcToPoint(context, 0, 0, RD(_topLeftRadius), 0, RD(_topLeftRadius));
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//- (UIEdgeInsets)insetsForSize:(CGSize)size {
// CGFloat fh = size.height/3;
// return UIEdgeInsetsMake(floor(MAX(RD(_topLeftRadius), RD(_topRightRadius))),
// floor(MAX(RD(_topLeftRadius), RD(_bottomLeftRadius))),
// floor(MAX(RD(_bottomLeftRadius), RD(_bottomRightRadius))),
// floor(MAX(RD(_topRightRadius), RD(_bottomRightRadius))));
//}
@end