-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJOBadgeView.m
More file actions
207 lines (167 loc) · 6.52 KB
/
Copy pathJOBadgeView.m
File metadata and controls
207 lines (167 loc) · 6.52 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
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
//
// JOBadgeView.m
// JOKit
//
// Created by Jeffrey Oloresisimo on 11-07-27.
// Copyright (c) 2011 Jeffrey Oloresisimo. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#import "JOBadgeView.h"
#define PADDING 14
@interface JOBadgeView (PRIVATE)
- (void)initialize;
- (void)autoPositionFromSuperView;
@end
@implementation JOBadgeView
@synthesize fillColor = _fillColor;
@synthesize borderColor = _borderColor;
@synthesize showBorder = _showBorder;
@synthesize showGloss = _showGloss;
@synthesize showShadow = _showShadow;
@synthesize autoPositioning = _autoPositioning;
- (id)init {
self = [super init];
if (self) {
[self initialize];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self initialize];
}
return self;
}
- (void)setFont:(UIFont*)font {
[super setFont:font];
[self setBadgeNeedsDisplay];
}
- (void)setText:(NSString*)text {
[super setText:text];
[self setBadgeNeedsDisplay];
}
- (void)setFillColor:(UIColor *)fillColor {
if (_fillColor != fillColor) {
[_fillColor release];
_fillColor = [fillColor copy];
}
[self setBadgeNeedsDisplay];
}
- (void)setBorderColor:(UIColor *)borderColor {
if (_borderColor != borderColor) {
[_borderColor release];
_borderColor = [borderColor copy];
}
[self setBadgeNeedsDisplay];
}
- (void)setShowShadow:(BOOL)shadow {
_showShadow = shadow;
[self setBadgeNeedsDisplay];
}
- (void)setBadgeNeedsDisplay {
CGSize size = [self.text sizeWithFont:self.font];
CGFloat radius = ceil((size.height)/2.0);
CGFloat adjustment = ceil(size.width-radius) > 0 ? ceil(size.width-radius) : 0;
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, (2*radius)+adjustment+PADDING, size.height+PADDING);
self.hidden = [self.text length] == 0;
self.layer.shadowOpacity = 0.5;
self.layer.masksToBounds = NO;
self.layer.shadowOffset = _showShadow ? CGSizeMake(0,floor(size.height/10)) : CGSizeMake(0,0);
[self setNeedsDisplay];
}
#pragma mark - PRIVATE
- (void)initialize {
self.font = [UIFont boldSystemFontOfSize:14];
self.textColor = [UIColor whiteColor];
self.backgroundColor = [UIColor clearColor];
self.textAlignment = UITextAlignmentCenter;
self.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
self.fillColor = [UIColor redColor];
self.borderColor = [UIColor whiteColor];
self.showBorder = YES;
self.showGloss = YES;
self.showShadow = YES;
self.autoPositioning = YES;
}
- (void)autoPositionFromSuperView {
CGSize size = [self superview].bounds.size;
self.frame = CGRectMake(size.width-self.bounds.size.width, self.bounds.origin.y, self.bounds.size.width, self.bounds.size.height);
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGFloat radius = ceilf((self.bounds.size.height/2.0)-6);
CGFloat minx = ceilf(CGRectGetMinX(self.bounds)+6), midx = ceilf(CGRectGetMidX(self.bounds)), maxx = ceilf(CGRectGetMaxX(self.bounds)-6);
CGFloat miny = ceilf(CGRectGetMinY(self.bounds)+6), midy = ceilf(CGRectGetMidY(self.bounds)), maxy = ceilf(CGRectGetMaxY(self.bounds)-6);
CGContextMoveToPoint(context, minx, midy);
CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
// fill
if (_fillColor) {
CGContextSetFillColorWithColor(context, _fillColor.CGColor);
}
// border
if (_showBorder) {
CGContextSetLineWidth(context, ceil((self.bounds.size.height-PADDING)*0.11));
CGContextSetStrokeColorWithColor(context, _borderColor.CGColor);
} else { // set border color the same as fill color
CGContextSetLineWidth(context, ceil((self.bounds.size.height-PADDING)*0.11));
CGContextSetStrokeColorWithColor(context, _fillColor.CGColor);
}
CGContextDrawPath(context, kCGPathFillStroke);
// gloss
if (_showGloss) {
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = {
1.0, 1.0, 1.0, 0.8,
1.0, 1.0, 1.0, 0.0,
};
CGColorSpaceRef cspace;
CGGradientRef gradient;
cspace = CGColorSpaceCreateDeviceRGB();
gradient = CGGradientCreateWithColorComponents(cspace, components, locations, 2);
CGFloat halfRadius = ceilf(midy/2.0);
CGContextMoveToPoint(context, minx, midy/2);
CGContextAddArcToPoint(context, minx, miny, midx, miny, halfRadius);
CGContextAddArcToPoint(context, maxx, miny, maxx, midy/2, halfRadius);
CGContextAddArcToPoint(context, maxx, midy, midx, midy, halfRadius);
CGContextAddArcToPoint(context, minx, midy, minx, midy/2, halfRadius);
CGContextClip(context);
CGContextDrawLinearGradient (context, gradient, CGPointMake(radius, 0), CGPointMake(radius, maxy), kCGGradientDrawsBeforeStartLocation);
CGColorSpaceRelease(cspace);
CGGradientRelease(gradient);
}
CGContextRestoreGState(context);
if (_autoPositioning) {
[self autoPositionFromSuperView];
}
[super drawRect:rect];
}
- (void)dealloc {
[_fillColor release];
[_borderColor release];
[super dealloc];
}
@end