-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathUIBadgeView.m
executable file
·200 lines (165 loc) · 7.38 KB
/
UIBadgeView.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
//
// UIBadgeView.m
// Coding_iOS
//
// Created by 王 原闯 on 14-8-6.
// Copyright (c) 2014年 Coding. All rights reserved.
//
#define kMaxBadgeWith 100.0
#define kBadgeTextOffset 2.0
#define kBadgePading 2.0
#import "UIBadgeView.h"
@interface UIBadgeView ()
/**
* Color used for badge's background.
*/
@property (strong) UIColor *badgeBackgroundColor;
/**
* Color used for badge's text.
*/
@property (strong) UIColor *badgeTextColor;
/**
* Font used for badge's text.
*/
@property (nonatomic) UIFont *badgeTextFont;
@end
@implementation UIBadgeView
+ (UIBadgeView *)viewWithBadgeTip:(NSString *)badgeValue{
if (!badgeValue || badgeValue.length == 0) {
return nil;
}
UIBadgeView *badgeV = [[UIBadgeView alloc] init];
badgeV.frame = [badgeV badgeFrameWithStr:badgeValue];
badgeV.badgeValue = badgeValue;
return badgeV;
}
+ (CGSize)badgeSizeWithStr:(NSString *)badgeValue font:(UIFont *)font{
if (!badgeValue || badgeValue.length == 0) {
return CGSizeZero;
}
if (!font) {
if (kDevice_Is_iPhone6 || kDevice_Is_iPhone6Plus) {
font = [UIFont systemFontOfSize:12];
}else{
font = [UIFont systemFontOfSize:11];
}
}
CGSize badgeSize = [badgeValue getSizeWithFont:font constrainedToSize:CGSizeMake(kMaxBadgeWith, 20)];
if (badgeSize.width < badgeSize.height) {
badgeSize = CGSizeMake(badgeSize.height, badgeSize.height);
}
if ([badgeValue isEqualToString:kBadgeTipStr]) {
badgeSize = CGSizeMake(4, 4);
}
return badgeSize;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self commonInitialization];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self commonInitialization];
}
return self;
}
- (id)init {
return [self initWithFrame:CGRectZero];
}
- (void)commonInitialization {
// Setup defaults
[self setBackgroundColor:[UIColor clearColor]];
_badgeBackgroundColor = [UIColor colorWithHexString:@"0xFF0000"];
_badgeTextColor = [UIColor whiteColor];
if (kDevice_Is_iPhone6 || kDevice_Is_iPhone6Plus) {
_badgeTextFont = [UIFont boldSystemFontOfSize:12];
}else{
_badgeTextFont = [UIFont boldSystemFontOfSize:11];
}
}
// Only override drawRect: if you perform custom drawing.
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
// Drawing code
// Draw badges
if ([[self badgeValue] length]) {
CGSize badgeSize = [self badgeSizeWithStr:_badgeValue];
CGRect badgeBackgroundFrame = CGRectMake(kBadgeTextOffset,
kBadgeTextOffset,
badgeSize.width + 2 * kBadgePading,
badgeSize.height + 2 * kBadgePading);
CGRect badgeBackgroundPaddingFrame = CGRectMake(0, 0, badgeBackgroundFrame.size.width +2*kBadgePading, badgeBackgroundFrame.size.height +2*kBadgePading);
if ([self badgeBackgroundColor]) {
if (![self.badgeValue isEqualToString:kBadgeTipStr]) {//外白色描边
CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
if (badgeSize.width > badgeSize.height) {
CGFloat circleWith = badgeBackgroundPaddingFrame.size.height;
CGFloat totalWidth = badgeBackgroundPaddingFrame.size.width;
CGFloat diffWidth = totalWidth - circleWith;
CGPoint originPoint = badgeBackgroundPaddingFrame.origin;
CGRect leftCicleFrame = CGRectMake(originPoint.x, originPoint.y, circleWith, circleWith);
CGRect centerFrame = CGRectMake(originPoint.x +circleWith/2, originPoint.y, diffWidth, circleWith);
CGRect rightCicleFrame = CGRectMake(originPoint.x +(totalWidth - circleWith), originPoint.y, circleWith, circleWith);
CGContextFillEllipseInRect(context, leftCicleFrame);
CGContextFillRect(context, centerFrame);
CGContextFillEllipseInRect(context, rightCicleFrame);
}else{
CGContextFillEllipseInRect(context, badgeBackgroundPaddingFrame);
}
}
// badge背景色
CGContextSetFillColorWithColor(context, [[self badgeBackgroundColor] CGColor]);
if (badgeSize.width > badgeSize.height) {
CGFloat circleWith = badgeBackgroundFrame.size.height;
CGFloat totalWidth = badgeBackgroundFrame.size.width;
CGFloat diffWidth = totalWidth - circleWith;
CGPoint originPoint = badgeBackgroundFrame.origin;
CGRect leftCicleFrame = CGRectMake(originPoint.x, originPoint.y, circleWith, circleWith);
CGRect centerFrame = CGRectMake(originPoint.x +circleWith/2, originPoint.y, diffWidth, circleWith);
CGRect rightCicleFrame = CGRectMake(originPoint.x +(totalWidth - circleWith), originPoint.y, circleWith, circleWith);
CGContextFillEllipseInRect(context, leftCicleFrame);
CGContextFillRect(context, centerFrame);
CGContextFillEllipseInRect(context, rightCicleFrame);
}else{
CGContextFillEllipseInRect(context, badgeBackgroundFrame);
}
}
//badgeValue
if (![self.badgeValue isEqualToString:kBadgeTipStr]) {
CGContextSetFillColorWithColor(context, [[self badgeTextColor] CGColor]);
NSMutableParagraphStyle *badgeTextStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
[badgeTextStyle setLineBreakMode:NSLineBreakByWordWrapping];
[badgeTextStyle setAlignment:NSTextAlignmentCenter];
NSDictionary *badgeTextAttributes = @{
NSFontAttributeName: [self badgeTextFont],
NSForegroundColorAttributeName: [self badgeTextColor],
NSParagraphStyleAttributeName: badgeTextStyle,
};
[[self badgeValue] drawInRect:CGRectMake(CGRectGetMinX(badgeBackgroundFrame) + kBadgeTextOffset,
CGRectGetMinY(badgeBackgroundFrame) + kBadgeTextOffset,
badgeSize.width, badgeSize.height)
withAttributes:badgeTextAttributes];
}
}
CGContextRestoreGState(context);
}
- (void)setBadgeValue:(NSString *)badgeValue {
_badgeValue = badgeValue;
self.frame = [self badgeFrameWithStr:badgeValue];
[self setNeedsDisplay];
}
- (CGSize)badgeSizeWithStr:(NSString *)badgeValue{
return [UIBadgeView badgeSizeWithStr:badgeValue font:self.badgeTextFont];
}
- (CGRect)badgeFrameWithStr:(NSString *)badgeValue{
CGSize badgeSize = [self badgeSizeWithStr:badgeValue];
CGRect badgeFrame = CGRectMake(0, 0, badgeSize.width +8, badgeSize.height +8);//8=2*2(红圈-文字)+2*2(白圈-红圈)
return badgeFrame;
}
@end