forked from matteogobbi/UIView-MGBadgeView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIView+MGBadgeView.m
269 lines (198 loc) · 7.97 KB
/
UIView+MGBadgeView.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
//
// UIView+MGBadgeView.m
// purl
//
// Created by Matteo Gobbi on 28/05/2014.
// Copyright (c) 2014 Matteo Gobbi (Voxygen). All rights reserved.
//
#import "UIView+MGBadgeView.h"
#import <objc/runtime.h>
@implementation MGBadgeView
static float const kMGBadgeViewInnerSpaceFromBorder = 7.0;
static int const kMGBadgeViewTag = 9876;
#define BADGE_SIDE_OFFSET kMGBadgeViewInnerSpaceFromBorder + _outlineWidth
#define BADGE_TOTAL_OFFSET BADGE_SIDE_OFFSET * 2.0
- (instancetype)initWithFrame:(CGRect)frame {
if(self = [super initWithFrame:frame]) {
_font = [UIFont boldSystemFontOfSize:13.0];
_badgeColor = [UIColor blueColor];
_textColor = [UIColor whiteColor];
_outlineColor = [UIColor whiteColor];
_outlineWidth = 2.0;
_minDiameter = 25.0;
_position = MGBadgePositionBest;
_displayIfZero = NO;
_horizontalOffset = 0.0;
_verticalOffset = 0.0;
self.backgroundColor = [UIColor clearColor];
self.opaque = YES;
self.tag = kMGBadgeViewTag;
}
return self;
}
- (void)drawRect:(CGRect)rect {
if(_badgeValue != 0 || _displayIfZero) {
NSString *stringToDraw = [NSString stringWithFormat:@"%ld", (long)_badgeValue];
CGContextRef context = UIGraphicsGetCurrentContext();
[_outlineColor set];
CGContextFillEllipseInRect(context, CGRectInset(rect, 1.0, 1.0));
[_badgeColor set];
CGContextFillEllipseInRect(context, CGRectInset(rect, _outlineWidth + 1.0, _outlineWidth + 1.0));
CGSize numberSize = [stringToDraw sizeWithAttributes:@{NSFontAttributeName: _font}];
[_textColor set];
NSMutableParagraphStyle *paragrapStyle = [NSMutableParagraphStyle new];
paragrapStyle.lineBreakMode = NSLineBreakByClipping;
paragrapStyle.alignment = NSTextAlignmentCenter;
CGRect lblRect = CGRectMake(rect.origin.x, (rect.size.height / 2.0) - (numberSize.height / 2.0), rect.size.width, numberSize.height);
[stringToDraw drawInRect:lblRect withAttributes:@{
NSFontAttributeName : _font,
NSParagraphStyleAttributeName : paragrapStyle,
NSForegroundColorAttributeName : _textColor
}];
}
}
#pragma mark - Properties accessor methods
- (void)setBadgeValue:(NSInteger)badgeValue {
if(_badgeValue != badgeValue) {
_badgeValue = badgeValue;
if(badgeValue != 0 || _displayIfZero) {
[self mg_updateBadgeViewSize];
if(_position == MGBadgePositionBest)
[self mg_updateBadgeViewPosition];
} else {
self.frame = CGRectZero;
}
[self setNeedsDisplay];
}
}
- (void)setPosition:(MGBadgePosition)position {
if(_position != position) {
_position = position;
[self mg_updateBadgeViewPosition];
[self setNeedsDisplay];
}
}
- (void)setMinDiameter:(float)minDiameter {
if (_minDiameter != minDiameter) {
_minDiameter = minDiameter;
if(_position == MGBadgePositionBest)
[self mg_updateBadgeViewPosition];
[self setNeedsDisplay];
}
}
- (void)setBadgeColor:(UIColor *)badgeColor {
if(_badgeColor != badgeColor) {
_badgeColor = badgeColor;
[self setNeedsDisplay];
}
}
- (void)setTextColor:(UIColor *)textColor {
if(_textColor != textColor) {
_textColor = textColor;
[self setNeedsDisplay];
}
}
- (void)setOutlineColor:(UIColor *)outlineColor {
if(_outlineColor != outlineColor) {
_outlineColor = outlineColor;
[self setNeedsDisplay];
}
}
- (void)setOutlineWidth:(float)outlineWidth {
if(_outlineWidth != outlineWidth) {
_outlineWidth = outlineWidth;
if(_position == MGBadgePositionBest)
[self mg_updateBadgeViewPosition];
[self setNeedsDisplay];
}
}
- (void)setFont:(UIFont *)font {
if(_font != font) {
_font = font;
[self mg_updateBadgeViewSize];
if(_position == MGBadgePositionBest)
[self mg_updateBadgeViewPosition];
[self setNeedsDisplay];
}
}
- (void)setDisplayIfZero:(BOOL)displayIfZero {
if(_displayIfZero != displayIfZero) {
_displayIfZero = displayIfZero;
if(_badgeValue == 0) {
if(_displayIfZero) {
[self mg_updateBadgeViewSize];
if(_position == MGBadgePositionBest)
[self mg_updateBadgeViewPosition];
} else {
self.frame = CGRectZero;
}
}
}
}
#pragma mark - Private methods
- (void)mg_updateBadgeViewSize {
//Calculate badge bounds
CGSize numberSize = [[NSString stringWithFormat:@"%ld", (long)_badgeValue] sizeWithAttributes:@{NSFontAttributeName: _font}];
float badgeHeight = MAX(BADGE_TOTAL_OFFSET + numberSize.height, _minDiameter);
float badgeWidth = MAX(badgeHeight, BADGE_TOTAL_OFFSET + numberSize.width);
[self setBounds:CGRectMake(0, 0, badgeWidth, badgeHeight)];
}
- (void)mg_updateBadgeViewPosition {
CGRect superviewFrame = self.superview.frame;
CGSize badgeSize = self.bounds.size;
MGBadgePosition position = _position;
//Set the best position before
if(position == MGBadgePositionBest) {
CGPoint topRightInWindow = [self.superview convertPoint:CGPointMake(superviewFrame.origin.x + superviewFrame.size.width + (badgeSize.width / 2.0), -(badgeSize.height / 2.0)) fromView:nil];
CGSize appFrameSize = [[UIScreen mainScreen] applicationFrame].size;
if(topRightInWindow.x > appFrameSize.width) {
position = (topRightInWindow.y < appFrameSize.height) ? MGBadgePositionBottomLeft : MGBadgePositionTopLeft;
} else {
position = (topRightInWindow.y < appFrameSize.height) ? MGBadgePositionBottomRight : MGBadgePositionTopRight;
}
}
switch (position) {
case MGBadgePositionCenterLeft: {
self.center = CGPointMake(_horizontalOffset, superviewFrame.size.height / 2 + _verticalOffset);
break;
}
case MGBadgePositionCenterRight: {
self.center = CGPointMake(superviewFrame.size.width + _horizontalOffset, superviewFrame.size.height / 2 + _verticalOffset);
break;
}
case MGBadgePositionTopRight: {
self.center = CGPointMake(superviewFrame.size.width + _horizontalOffset, _verticalOffset);
break;
}
case MGBadgePositionTopLeft: {
self.center = CGPointMake(_horizontalOffset, _verticalOffset);
break;
}
case MGBadgePositionBottomRight: {
self.center = CGPointMake(superviewFrame.size.width + _horizontalOffset, superviewFrame.size.height + _verticalOffset);
break;
}
case MGBadgePositionBottomLeft: {
self.center = CGPointMake(_horizontalOffset, superviewFrame.size.height + _verticalOffset);
break;
}
default:
break;
}
}
@end
static const char *kMGBadgeViewPropertyKey = "kMGBadgeViewPropertyKey";
@implementation UIView (MGBadgeView)
- (void)setBadgeView:(MGBadgeView *)badgeView {
objc_setAssociatedObject(self, kMGBadgeViewPropertyKey, badgeView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (MGBadgeView *)badgeView {
MGBadgeView *badgeView = objc_getAssociatedObject(self, kMGBadgeViewPropertyKey);
if (!badgeView) {
self.badgeView = [[MGBadgeView alloc] initWithFrame:CGRectZero];
badgeView = objc_getAssociatedObject(self, kMGBadgeViewPropertyKey);
[self addSubview:badgeView];
}
return badgeView;
}
@end