forked from TanguyAladenise/BBBadgeBarButtonItem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBBBadgeBarButtonItem.m
More file actions
174 lines (136 loc) · 4.99 KB
/
BBBadgeBarButtonItem.m
File metadata and controls
174 lines (136 loc) · 4.99 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
//
// BBBadgeBarButtonItem.m
// Riverie
//
// Created by Tanguy Aladenise on 07/02/14.
// Copyright (c) 2014 Riverie, Inc. All rights reserved.
//
#import "BBBadgeBarButtonItem.h"
// Set a padding for the badge
static int const BBBadgeMargin = 4;
// Avoid badge to small
static int const BBMinSize = 8;
// Default offset for the badge
// Change things here if your picto doesn't fit this settings
static int const BBoriginX = 17;
static int const BBoriginY = -9;
@interface BBBadgeBarButtonItem()
// The badge over the BarButtonitem
@property (nonatomic) UILabel *badge;
@end
@implementation BBBadgeBarButtonItem
#pragma mark - Init methods
- (BBBadgeBarButtonItem *)initWithCustomUIButton:(UIButton *)customButton
{
self = [self initWithCustomView:customButton];
if (self) {
[self initializer];
}
return self;
}
- (void)initializer
{
// Default design initialization
self.badgeBGColor = [UIColor redColor];
self.badgeTextColor = [UIColor whiteColor];
self.badgeFont = [UIFont fontWithName:@"Helvetica" size:12];
self.shouldHideBadgeAtZero = YES;
self.shouldAnimateBadge = YES;
self.badgeValue = @"0";
}
#pragma mark - Utility methods
// Handle badge display when its properties have been changed (color, font, ...)
- (void)refreshBadge
{
// Change new attributes
self.badge.textColor = self.badgeTextColor;
self.badge.backgroundColor = self.badgeBGColor;
self.badge.font = self.badgeFont;
}
// Handle the badge changing value
- (void)updateBadgeValue
{
// Bounce animation on badge if value changed and if animation authorized
if (self.shouldAnimateBadge && ![self.badge.text isEqualToString:self.badgeValue]) {
CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
[animation setFromValue:[NSNumber numberWithFloat:1.5]];
[animation setToValue:[NSNumber numberWithFloat:1]];
[animation setDuration:.5];
[animation setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:.4 :1.3 :1 :1]];
[self.badge.layer addAnimation:animation forKey:@"bounceAnimation"];
}
// Set the new value
self.badge.text = self.badgeValue;
self.badge.clipsToBounds = YES;
// When the value changes the badge could need to get bigger
// Calculate expected size to fit new value
// Use an intermediate label to get expected size thanks to sizeToFit
// We don't call sizeToFit on the true label to avoid bad display
UILabel *frameLabel = [self duplicateLabel:self.badge];
[frameLabel sizeToFit];
CGSize expectedLabelSize = frameLabel.frame.size;
// Make sure that for small value, the badge will be big enough
float minHeight = expectedLabelSize.height;
// Using const we make sure the badge doesn't get too smal
minHeight = (minHeight < BBMinSize) ? BBMinSize : expectedLabelSize.height;
float minWidth = expectedLabelSize.width;
// Using const we make sure the badge doesn't get too smal
minWidth = (minWidth < minHeight) ? minHeight : expectedLabelSize.width;
// Animate the size modification just in case
[UIView animateWithDuration:0.2 animations:^{
self.badge.frame = CGRectMake(BBoriginX, BBoriginY, minWidth + BBBadgeMargin, minHeight + BBBadgeMargin);
self.badge.layer.cornerRadius = (minHeight + BBBadgeMargin) / 2;
}];
}
- (UILabel *)duplicateLabel:(UILabel *)labelToCopy
{
UILabel *duplicateLabel = [[UILabel alloc] initWithFrame:labelToCopy.frame];
duplicateLabel.text = labelToCopy.text;
duplicateLabel.font = labelToCopy.font;
return duplicateLabel;
}
#pragma mark - Setters
- (void)setBadgeValue:(NSString *)badgeValue
{
// Set new value
_badgeValue = badgeValue;
// When changing the badge value check if we need to remove the badge
if ([badgeValue isEqualToString:@"0"] && self.shouldHideBadgeAtZero) {
[self.badge removeFromSuperview];
self.badge = nil;
return;
}
// Otwhersise check if badge already exists
else if(!self.badge) {
// Create a new badge
self.badge = [[UILabel alloc] initWithFrame:CGRectMake(BBoriginX, BBoriginY, 20, 20)];
self.badge.textColor = self.badgeTextColor;
self.badge.backgroundColor = self.badgeBGColor;
self.badge.font = self.badgeFont;
self.badge.textAlignment = NSTextAlignmentCenter;
[self.customView addSubview:self.badge];
}
[self updateBadgeValue];
}
- (void)setBadgeBGColor:(UIColor *)badgeBGColor
{
_badgeBGColor = badgeBGColor;
if (self.badge) {
[self refreshBadge];
}
}
- (void)setBadgeTextColor:(UIColor *)badgeTextColor
{
_badgeTextColor = badgeTextColor;
if (self.badge) {
[self refreshBadge];
}
}
- (void)setBadgeFont:(UIFont *)badgeFont
{
_badgeFont = badgeFont;
if (self.badge) {
[self refreshBadge];
}
}
@end