-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDKTextResizingLabel.m
43 lines (30 loc) · 1008 Bytes
/
DKTextResizingLabel.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
//
// DKTextResizingLabel.m
//
// Created by Dominik Krejcik on 7/2/12.
#import "DKTextResizingLabel.h"
@implementation DKTextResizingLabel
@synthesize maxFontSize;
-(DKTextResizingLabel*)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
//Default maxFontSize
self.maxFontSize = 40;
}
return self;
}
-(void)setText:(NSString *)text {
[super setText:text];
if (self.numberOfLines > 1) {
UIFont *font = self.font;
for (int i = (int)self.maxFontSize; i >= self.minimumFontSize; i--) {
font = [font fontWithSize:i];
CGSize constraintSize = CGSizeMake(self.frame.size.width, MAXFLOAT);
CGSize labelSize = [self.text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
if (labelSize.height <= self.frame.size.height)
break;
}
self.font = font;
}
}
@end