forked from nschum/FontAwesomeIconFactory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNIKFontAwesomePathRenderer.m
76 lines (59 loc) · 2.02 KB
/
NIKFontAwesomePathRenderer.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
#import "NIKFontAwesomePathRenderer.h"
@implementation NIKFontAwesomePathRenderer
- (void)dealloc {
self.path = NULL;
self.strokeColor = NULL;
}
- (void)setPath:(CGPathRef)path {
if (_path) {
CGPathRelease(_path);
}
_path = path ? CGPathCreateCopy(path) : NULL;
}
- (void)setStrokeColor:(CGColorRef)strokeColor {
if (_strokeColor) {
CGColorRelease(_strokeColor);
}
_strokeColor = strokeColor ? CGColorCreateCopy(strokeColor) : NULL;
}
#pragma mark -
- (void)renderInContext:(CGContextRef)context {
CGRect bounds = CGPathGetBoundingBox(_path);
CGContextTranslateCTM(context, _offset.x - bounds.origin.x, _offset.y - bounds.origin.y);
CGContextAddPath(context, _path);
if (_colors.count > 1) {
CGContextSaveGState(context);
CGContextClip(context);
[self renderGradientInRect:bounds context:context];
CGContextRestoreGState(context);
} else {
CGContextSetFillColorWithColor(context, (__bridge CGColorRef)_colors[0]);
CGContextFillPath(context);
}
CGContextAddPath(context, _path);
if (_strokeColor && _strokeWidth > 0.0) {
CGContextSetStrokeColorWithColor(context, _strokeColor);
CGContextSetLineWidth(context, _strokeWidth);
CGContextStrokePath(context);
}
}
- (void)renderGradientInRect:(CGRect)rect
context:(CGContextRef)context {
NSUInteger n = _colors.count;
CGFloat locations[n];
for (NSUInteger i = 0; i < n; i++) {
locations[i] = (CGFloat)i / (n - 1);
}
CGGradientRef gradient =
CGGradientCreateWithColors(NULL, (__bridge CFArrayRef)_colors, locations);
CGContextDrawLinearGradient(context, gradient, CGRectGetTopLeft(rect),
CGRectGetBottomLeft(rect), 0);
CGGradientRelease(gradient);
}
static CGPoint CGRectGetBottomLeft(CGRect rect) {
return rect.origin;
}
static CGPoint CGRectGetTopLeft(CGRect rect) {
return CGPointMake(rect.origin.x, rect.origin.y + rect.size.height);
}
@end