-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathUITabBarItem+CustomBadge.m
81 lines (51 loc) · 2.07 KB
/
UITabBarItem+CustomBadge.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
//
// UITabBarItem+CustomBadge.m
// CityGlance
//
// Created by Enrico Vecchio on 18/05/14.
// Copyright (c) 2014 Cityglance SRL. All rights reserved.
//
#import "UITabBarItem+CustomBadge.h"
#define CUSTOM_BADGE_TAG 99
#define OFFSET 0.6f
@implementation UITabBarItem (CustomBadge)
-(void) setMyAppCustomBadgeValue: (NSString *) value
{
UIFont *myAppFont = [UIFont systemFontOfSize:13.0];
UIColor *myAppFontColor = [UIColor whiteColor];
UIColor *myAppBackColor = [UIColor lightGrayColor];
[self setCustomBadgeValue:value withFont:myAppFont andFontColor:myAppFontColor andBackgroundColor:myAppBackColor];
}
-(void) setCustomBadgeValue: (NSString *) value withFont: (UIFont *) font andFontColor: (UIColor *) color andBackgroundColor: (UIColor *) backColor
{
UIView *v = (UIView *)[self performSelector:@selector(view)];
[self setBadgeValue:value];
for(UIView *sv in v.subviews)
{
NSString *str = NSStringFromClass([sv class]);
if([str isEqualToString:@"_UIBadgeView"])
{
for(UIView *ssv in sv.subviews)
{
// REMOVE PREVIOUS IF EXIST
if(ssv.tag == CUSTOM_BADGE_TAG) { [ssv removeFromSuperview]; }
}
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, sv.frame.size.width, sv.frame.size.height)];
[l setFont:font];
[l setText:value];
[l setBackgroundColor:backColor];
[l setTextColor:color];
[l setTextAlignment:NSTextAlignmentCenter];
l.layer.cornerRadius = l.frame.size.height/2;
l.layer.masksToBounds = YES;
// Fix for border
sv.layer.borderWidth = 1;
sv.layer.borderColor = [backColor CGColor];
sv.layer.cornerRadius = sv.frame.size.height/2;
sv.layer.masksToBounds = YES;
[sv addSubview:l];
l.tag = CUSTOM_BADGE_TAG;
}
}
}
@end