-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBBIconLabel.m
79 lines (64 loc) · 1.7 KB
/
BBIconLabel.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
//
// BBIconLabel.m
// BButtonDemo
//
// Created by Thomas on 08.06.13.
// Copyright (c) 2013 Hexed Bits. All rights reserved.
//
#import "BBIconLabel.h"
//NSString* BButton_iconFontName = @"icomoon";
NSString* BBIconLabel_iconFontName = @"FontAwesome";
NSDictionary* BBIconLabel_textToIconsMap = nil;
unsigned int BBIconLabel_padding = 4;
@implementation BBIconLabel
+ (void) setTextToIconMap:(NSDictionary*)map // translation of setText text to icon
{
BBIconLabel_textToIconsMap = map;
}
+ (void) setIconFontName:(NSString*)fontName // changes the icon font for BButton globally
{
BBIconLabel_iconFontName = fontName;
}
+ (void) setPadding:(unsigned int)padding // change the default border padding of 4!
{
BBIconLabel_padding = padding;
}
- (void)awakeFromNib
{
[super awakeFromNib];
[self setText:self.text];
}
- (void) layoutSubviews
{
[super layoutSubviews];
[self adjustFont];
}
- (void) adjustFont // resize every time the label changes itself
{
int h = self.frame.size.height - BBIconLabel_padding;
if (self.frame.size.height < 0) // frame is not ready in -awakeFromNib
h = self.font.pointSize;
UIFont* f = [UIFont fontWithName:BBIconLabel_iconFontName size:h];
#ifdef DEBUG
if (f == nil)
{
NSLog(@"BButton Font '%@' not found! Check if font is in Info.plist.",BBIconLabel_iconFontName);
}
#endif
self.font = f;
}
- (void)setText:(NSString *)text
{
if(BBIconLabel_textToIconsMap)
{
NSString* s = [BBIconLabel_textToIconsMap objectForKey:text];
if(s != nil)
{
[super setText:s];
[self adjustFont];
return;
}
}
[super setText:text];
}
@end