forked from yonat/BadgeLabel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BadgeTableViewCell.m
63 lines (52 loc) · 1.33 KB
/
BadgeTableViewCell.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
//
// BadgeTableViewCell.m
// Contacts Reminders
//
// Created by Yonat Sharon on 1/2/12.
// Copyright (c) 2012 Roy Sharon Ltd. All rights reserved.
//
#import "BadgeTableViewCell.h"
#import "BadgeLabel.h"
@implementation BadgeTableViewCell
@synthesize badge;
- (void)layoutBadge
{
if (badge == nil || badge.hidden || [badge.text length] == 0) return;
CGRect badgeFrame = badge.frame;
CGRect contentBounds = self.contentView.bounds;
badgeFrame.origin.y = contentBounds.origin.y + (contentBounds.size.height - badgeFrame.size.height) / 2;
badgeFrame.origin.x = contentBounds.origin.x + contentBounds.size.width - badgeFrame.size.width - 8;
badge.frame = badgeFrame;
}
- (void)createBadge
{
self.badge = [[BadgeLabel alloc] init];
[badge setStyle:BadgeLabelStyleMail];
[self.contentView addSubview:badge];
}
- (NSInteger)badgeNumber
{
if (badge && !badge.hidden) {
return [badge.text integerValue];
}
return 0;
}
- (void)setBadgeNumber:(NSInteger)badgeNumber
{
if (badgeNumber == 0) {
badge.hidden = YES;
return;
}
if (!badge) {
[self createBadge];
}
badge.text = [NSString stringWithFormat:@"%d", badgeNumber];
badge.hidden = NO;
[self layoutBadge];
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self layoutBadge];
}
@end