-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathTKRoundedView.m
479 lines (383 loc) · 14.7 KB
/
TKRoundedView.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//
// TKRoundedView.m
// TKRoundedView
//
// Created by Tomasz Kuźma on 1/6/13.
// Copyright (c) 2013 Tomasz Kuźma. All rights reserved.
//
#import "TKRoundedView.h"
#import <QuartzCore/QuartzCore.h>
const TKRoundedCorner TKRoundedCornerAll = TKRoundedCornerTopRight | TKRoundedCornerBottomRight | TKRoundedCornerBottomLeft | TKRoundedCornerTopLeft;
const TKDrawnBorderSides TKDrawnBorderSidesAll = TKDrawnBorderSidesRight | TKDrawnBorderSidesLeft | TKDrawnBorderSidesTop | TKDrawnBorderSidesBottom;
UIImage * TKRoundedCornerImage(CGSize size,
TKRoundedCorner corners,
TKDrawnBorderSides drawnBorders,
UIColor *fillColor,
UIColor *borderColor,
CGFloat borderWidth,
CGFloat cornerRadius){
TKRoundedView *view = [[TKRoundedView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, size.width, size.height)];
view.roundedCorners = corners;
view.drawnBordersSides = drawnBorders;
view.fillColor = fillColor;
view.borderColor = borderColor;
view.borderWidth = borderWidth;
view.cornerRadius = cornerRadius;
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
view = nil;
return img;
}
@interface TKRoundedView (){
CGColorSpaceRef _colorSpace;
CGFloat* _locationsTable;
CFArrayRef _cfColors;
CGGradientRef _gradient;
NSArray *_observableKeys;
}
@end
@implementation TKRoundedView
#pragma mark - Initialization
- (void)dealloc{
[self.observableKeys enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[self removeObserver:self forKeyPath:obj];
}];
if(_locationsTable != NULL)
free(_locationsTable);
if (_cfColors != NULL) {
CFRelease(_cfColors);
}
if (_colorSpace != NULL) {
CGColorSpaceRelease(_colorSpace);
}
if (_gradient != NULL) {
CGGradientRelease(_gradient);
}
}
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (!self)return nil;
[self commonInit];
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (!self)return nil;
[self commonInit];
return self;
}
- (void)commonInit{
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
self.contentMode = UIViewContentModeRedraw;
_gradientDirection = TKGradientDirectionVertical;
_fillColor = [UIColor whiteColor];
_borderColor = [UIColor lightGrayColor];
_cornerRadius = 15.0f;
_borderWidth = 1.0f;
_roundedCorners = TKRoundedCornerAll;
_drawnBordersSides = TKDrawnBorderSidesAll;
[self.observableKeys enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[self addObserver:self forKeyPath:obj options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:nil];
}];
}
#pragma mark - Drawing
- (void)drawRect:(CGRect)rect{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGFloat halfLineWidth = _borderWidth / 2.0f;
CGFloat topInsets = _drawnBordersSides & TKDrawnBorderSidesTop ? halfLineWidth : -halfLineWidth;
CGFloat leftInsets = _drawnBordersSides & TKDrawnBorderSidesLeft ? halfLineWidth : -halfLineWidth;
CGFloat rightInsets = _drawnBordersSides & TKDrawnBorderSidesRight ? halfLineWidth : -halfLineWidth;
CGFloat bottomInsets = _drawnBordersSides & TKDrawnBorderSidesBottom ? halfLineWidth : -halfLineWidth;
UIEdgeInsets insets = UIEdgeInsetsMake(topInsets, leftInsets, bottomInsets, rightInsets);
CGRect properRect = UIEdgeInsetsInsetRect(rect, insets);
/* Setup line width */
CGContextSetLineWidth(ctx, 0.0f);
// Add and fill rect
[self addPathToContext:ctx inRect:properRect respectDrawnBorder:NO];
// Close the path
CGContextClosePath(ctx);
if (_gradientColorsAndLocations.count) {
CGContextSaveGState(ctx);
CGContextClip(ctx);
[self drawGradientToContext:ctx inRect:rect];
CGContextRestoreGState(ctx);
}
else{
// Fill and Stroke path
CGContextSetFillColorWithColor(ctx, _fillColor.CGColor);
CGContextDrawPath(ctx, kCGPathFill);
}
/* Setup colors and line width */
CGContextSetStrokeColorWithColor(ctx, _borderColor.CGColor);
CGContextSetLineWidth(ctx, _borderWidth);
// Add and stroke rect
[self addPathToContext:ctx inRect:properRect respectDrawnBorder:YES];
// Fill and Stroke path
CGContextDrawPath(ctx, kCGPathStroke);
}
- (void)addPathToContext:(CGContextRef)ctx inRect:(CGRect)rect respectDrawnBorder:(BOOL)respectDrawnBorders{
CGFloat minx = CGRectGetMinX(rect);
CGFloat midx = CGRectGetMidX(rect);
CGFloat maxx = CGRectGetMaxX(rect);
CGFloat miny = CGRectGetMinY(rect);
CGFloat midy = CGRectGetMidY(rect);
CGFloat maxy = CGRectGetMaxY(rect);
CGContextMoveToPoint(ctx, minx, midy);
/* Top Left Corner */
if (_roundedCorners & TKRoundedCornerTopLeft && _drawnBordersSides & (TKDrawnBorderSidesTop | TKDrawnBorderSidesLeft)) {
CGContextAddArcToPoint(ctx, minx, miny, midx, miny, _cornerRadius);
CGContextAddLineToPoint(ctx, midx, miny);
}
else{
if (_drawnBordersSides & TKDrawnBorderSidesLeft || !respectDrawnBorders){
CGContextAddLineToPoint(ctx, minx, miny);
}
else{
CGContextDrawPath(ctx, kCGPathStroke);
CGContextMoveToPoint(ctx, minx, miny);
}
if (_drawnBordersSides & TKDrawnBorderSidesTop || !respectDrawnBorders){
CGContextAddLineToPoint(ctx, midx, miny);
}
else{
CGContextDrawPath(ctx, kCGPathStroke);
CGContextMoveToPoint(ctx, midx, miny);
}
}
/* Top Right Corner */
if (_roundedCorners & TKRoundedCornerTopRight && _drawnBordersSides & (TKDrawnBorderSidesTop | TKDrawnBorderSidesRight)) {
CGContextAddArcToPoint(ctx, maxx, miny, maxx, midy, _cornerRadius);
CGContextAddLineToPoint(ctx, maxx, midy);
}
else{
if (_drawnBordersSides & TKDrawnBorderSidesTop || !respectDrawnBorders){
CGContextAddLineToPoint(ctx, maxx, miny);
}
else{
CGContextDrawPath(ctx, kCGPathStroke);
CGContextMoveToPoint(ctx, maxx, miny);
}
if (_drawnBordersSides & TKDrawnBorderSidesRight || !respectDrawnBorders){
CGContextAddLineToPoint(ctx, maxx, midy);
}
else{
CGContextDrawPath(ctx, kCGPathStroke);
CGContextMoveToPoint(ctx, maxx, midy);
}
}
/* Bottom Right Corner */
if (_roundedCorners & TKRoundedCornerBottomRight && _drawnBordersSides & (TKDrawnBorderSidesBottom | TKDrawnBorderSidesRight)) {
CGContextAddArcToPoint(ctx, maxx, maxy, midx, maxy, _cornerRadius);
CGContextAddLineToPoint(ctx, midx, maxy);
}
else{
if (_drawnBordersSides & TKDrawnBorderSidesRight || !respectDrawnBorders){
CGContextAddLineToPoint(ctx, maxx, maxy);
}
else{
CGContextDrawPath(ctx, kCGPathStroke);
CGContextMoveToPoint(ctx, maxx, maxy);
}
if (_drawnBordersSides & TKDrawnBorderSidesBottom || !respectDrawnBorders){
CGContextAddLineToPoint(ctx, midx, maxy);
}
else{
CGContextDrawPath(ctx, kCGPathStroke);
CGContextMoveToPoint(ctx, midx, maxy);
}
}
/* Bottom Left Corner */
if (_roundedCorners & TKRoundedCornerBottomLeft && _drawnBordersSides & (TKDrawnBorderSidesBottom | TKDrawnBorderSidesLeft)) {
CGContextAddArcToPoint(ctx, minx, maxy, minx, midy, _cornerRadius);
CGContextAddLineToPoint(ctx, minx, midy);
}
else{
if (_drawnBordersSides & TKDrawnBorderSidesBottom || !respectDrawnBorders){
CGContextAddLineToPoint(ctx, minx, maxy);
}
else{
CGContextDrawPath(ctx, kCGPathStroke);
CGContextMoveToPoint(ctx, minx, maxy);
}
if (_drawnBordersSides & TKDrawnBorderSidesLeft || !respectDrawnBorders){
CGContextAddLineToPoint(ctx, minx, midy);
}
else{
CGContextMoveToPoint(ctx, minx, midy);
CGContextDrawPath(ctx, kCGPathStroke);
}
}
}
- (void)drawGradientToContext:(CGContextRef)ctx inRect:(CGRect)rect{
if (_gradient == NULL) {
_gradient = CGGradientCreateWithColors(_colorSpace,_cfColors, _locationsTable);
}
CGPoint startPoint = CGPointZero;
CGPoint endPoint = CGPointZero;
switch (_gradientDirection) {
case TKGradientDirectionVertical:
startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
break;
case TKGradientDirectionHorizontal:
startPoint = CGPointMake(CGRectGetMinX(rect), CGRectGetMidY(rect));
endPoint = CGPointMake(CGRectGetMaxX(rect), CGRectGetMidY(rect));
break;
case TKGradientDirectionDown:
startPoint = CGPointMake(CGRectGetMinX(rect), CGRectGetMinY(rect));
endPoint = CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect));
break;
case TKGradientDirectionUp:
startPoint = CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect));
endPoint = CGPointMake(CGRectGetMinX(rect), CGRectGetMinY(rect));
break;
default:
break;
}
CGContextSaveGState(ctx);
CGContextAddRect(ctx, rect);
CGContextClip(ctx);
CGContextDrawLinearGradient(ctx, _gradient, startPoint, endPoint, 0);
CGContextRestoreGState(ctx);
}
//#pragma mark - Setters
//
//- (void)setDrawnBordersSides:(TKDrawnBorderSides)drawnBordersSides{
// _drawnBordersSides = drawnBordersSides;
// [self setNeedsDisplay];
//}
//
//- (void)setRoundedCorners:(TKRoundedCorner)roundedCorners{
// _roundedCorners = roundedCorners;
// [self setNeedsDisplay];
//}
//
//- (void)setBorderColor:(UIColor *)borderColor{
// if (_borderColor != borderColor) {
// _borderColor = borderColor;
// [self setNeedsDisplay];
// }
//}
//
//- (void)setFillColor:(UIColor *)fillColor{
// if (_fillColor != fillColor) {
// _fillColor = fillColor;
// [self setNeedsDisplay];
// }
//}
//
//- (void)setGradientColorsAndLocations:(NSArray *)gradientColorsAndLocations{
// if (_gradientColorsAndLocations != gradientColorsAndLocations) {
// _gradientColorsAndLocations = gradientColorsAndLocations;
// [self prepareGradient];
// [self setNeedsDisplay];
// }
//}
//
//- (void)setGradientDirection:(TKGradientDirection)gradientDirection{
// if (_gradientDirection != gradientDirection) {
// _gradientDirection = gradientDirection;
// [self setNeedsDisplay];
// }
//}
//
//- (void)setBorderWidth:(CGFloat)borderWidth{
// _borderWidth = borderWidth;
// [self setNeedsDisplay];
//}
//
//- (void)setCornerRadius:(CGFloat)cornerRadius{
// _cornerRadius = cornerRadius;
// [self setNeedsDisplay];
//}
#pragma mark - Key-Value Observing
+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key{
return YES;
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"gradientColorsAndLocations"])
{
[self prepareGradient];
[self setNeedsDisplay];
return ;
}
[self.observableKeys enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
if ([keyPath isEqualToString:obj])
{
[self setNeedsDisplay];
return;
}
}];
}
- (void)didChange:(NSKeyValueChange)changeKind valuesAtIndexes:(NSIndexSet *)indexes forKey:(NSString *)key{
if ([key isEqualToString:@"gradientColorsAndLocations"])
{
[self prepareGradient];
[self setNeedsDisplay];
}
}
- (NSArray *)observableKeys{
if (!_observableKeys) {
_observableKeys = (@[
@"drawnBordersSides",
@"roundedCorners",
@"fillColor",
@"borderColor",
@"borderWidth",
@"cornerRadius",
@"gradientDirection",
@"gradientColorsAndLocations"
]);
}
return _observableKeys;
}
#pragma mark - Private
- (void)prepareGradient{
NSMutableArray *colors = [NSMutableArray arrayWithCapacity:_gradientColorsAndLocations.count];
NSMutableArray *locations = [NSMutableArray arrayWithCapacity:_gradientColorsAndLocations.count];
for (NSDictionary *dictionary in self.gradientColorsAndLocations) {
if ([dictionary isKindOfClass:[NSDictionary class]]) {
for (NSString *key in dictionary) {
id object = dictionary[key];
if ([object isKindOfClass:[NSNumber class]]) {
[locations addObject:object];
}
else if ([object isKindOfClass:[UIColor class]]){
[colors addObject:object];
}
}
}
}
if (colors.count == locations.count) {
if (_colorSpace == NULL) {
_colorSpace = CGColorSpaceCreateDeviceRGB();
}
NSInteger count = locations.count;
if (_locationsTable != NULL) {
free(_locationsTable);
}
_locationsTable = malloc((size_t) sizeof(CGFloat) * count);
CFMutableArrayRef cfColorsMutable = CFArrayCreateMutable(kCFAllocatorDefault, count, &kCFTypeArrayCallBacks);
for(int i = 0; i < count; i++){
NSNumber *locationNumber = locations[i];
_locationsTable[i] = [locationNumber floatValue];
CGColorRef color = [colors[i] CGColor];
CFArrayAppendValue(cfColorsMutable, color);
}
if (_cfColors != NULL) {
CFRelease(_cfColors);
}
_cfColors = CFArrayCreateCopy(kCFAllocatorDefault, cfColorsMutable);
CFRelease(cfColorsMutable);
if (_gradient != NULL) {
CGGradientRelease(_gradient);
_gradient = NULL;
}
}
}
@end