-
Notifications
You must be signed in to change notification settings - Fork 3
/
iDUBadgeButton.m
49 lines (48 loc) · 1.6 KB
/
iDUBadgeButton.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
#import "iDUBadgeButton.h"
@implementation iDUBadgeButton
- (instancetype)init {
self = [super init];
[self setup];
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
[self setup];
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
[self setup];
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
[_badgeLabel sizeToFit];
CGFloat height = MAX(18, (CGFloat)_badgeLabel.frame.size.height + 5.0);
CGFloat width = MAX(height, (CGFloat)_badgeLabel.frame.size.width + 10.0);
_badgeLabel.frame = CGRectMake(self.frame.size.width - 5, -_badgeLabel.frame.size.height / 2, width, height);
_badgeLabel.layer.cornerRadius = _badgeLabel.frame.size.height / 2;
_badgeLabel.layer.masksToBounds = true;
}
- (void)setup {
_badgeLabel = [UILabel new];
_badgeLabel.textColor = [UIColor whiteColor];
_badgeLabel.backgroundColor = [UIColor systemRedColor];
_badgeLabel.textAlignment = NSTextAlignmentCenter;
_badgeLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCaption1];
_badgeLabel.alpha = 0;
[self addSubview:_badgeLabel];
[self updateBadge];
}
- (void)setBadgeCount:(NSUInteger)badgeCount {
_badgeCount = badgeCount;
[self updateBadge];
}
- (void)updateBadge {
if (_badgeCount != 0) _badgeLabel.text = _badgeCount == 0 ? @" " : [NSString stringWithFormat:@"%lu", _badgeCount];
[UIView animateWithDuration:0.25 animations:^{
_badgeLabel.alpha = self.badgeCount == 0 ? 0 : 1;
}];
[self layoutSubviews];
}
@end