-
Notifications
You must be signed in to change notification settings - Fork 3
/
JPIconLabel.m
103 lines (99 loc) · 3.92 KB
/
JPIconLabel.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//
// JPIconLabel.m
// StackAsk
//
// Created by Jonathan Bailey on 24/09/2011.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
#import "JPIconLabel.h"
@implementation JPIconLabel
@synthesize textLabel, imageView;
@synthesize touchDown, touchUpInside, touchUpOutside;
- (void)resizeFontForLabel:(UILabel*)aLabel maxSize:(int)maxSize minSize:(int)minSize {
UIFont *font = aLabel.font;
if (!font) {
font = [UIFont systemFontOfSize:maxSize];
}
for(int i = maxSize; i >= minSize; i--) {
font = [font fontWithSize:i];
CGSize constraintSize = CGSizeMake(aLabel.frame.size.width, MAXFLOAT);
CGSize labelSize = [aLabel.text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
if(labelSize.height <= aLabel.frame.size.height)
break;
}
aLabel.font = font;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addObserver:self forKeyPath:@"textLabel.text" options:0 context:nil];
EGOImageView *image = [[EGOImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.height, self.frame.size.height)];
[image setContentMode:UIViewContentModeScaleAspectFit];
self.imageView = image;
[image release];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxY(image.frame) + 2, 0, self.frame.size.width - image.frame.size.width - 2, self.frame.size.height)];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setNumberOfLines:2];
[label setFont:[UIFont boldSystemFontOfSize:label.font.pointSize]];
[label setShadowColor:[UIColor colorWithRed:(0x45 / 255.0) green:(0x4E / 255.0) blue:(0x62 / 255.0) alpha:1.0]];
[label setShadowOffset:CGSizeMake(0, -1)];
[label setAdjustsFontSizeToFitWidth:YES];
self.textLabel = label;
[label release];
[self addSubview:self.imageView];
[self addSubview:self.textLabel];
}
return self;
}
-(void)setFrame:(CGRect)frame {
CGRect frameToSet = frame;
if (frame.size.width > 190) {
frameToSet = CGRectSetWidth(frameToSet, 190);
}
[super setFrame:frameToSet];
[self resizeFontForLabel:self.textLabel maxSize:17 minSize:10];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"textLabel.text"]) {
[self.textLabel sizeToFit];
[self.textLabel setFrame:CGRectSetHeight(self.textLabel.frame, self.frame.size.height)];
}
}
-(void)layoutSubviews {
self.bounds = CGRectSetWidth(self.bounds, self.textLabel.frame.size.width + self.imageView.frame.size.width);
self.frame = CGRectSetX(self.frame, roundf(self.frame.origin.x)-(self.frame.size.height/5));
if (self.frame.origin.x < 0) {
[self setFrame:CGRectSetX(self.frame, 0)];
}
if (self.frame.size.width > self.superview.frame.size.width) {
[self setFrame:CGRectSetWidth(self.frame, self.superview.frame.size.width)];
}
self.textLabel.frame = CGRectSetWidth(self.textLabel.frame, self.frame.size.width - self.imageView.frame.size.width);
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (touchDown) {
touchDown(touches, event);
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (CGRectContainsPoint(self.bounds, [(UITouch *)[touches anyObject] locationInView:self])) {
if (touchUpInside) {
touchUpInside(touches, event);
}
} else {
if (touchUpOutside) {
touchUpOutside(touches, event);
}
}
}
-(void)dealloc {
[textLabel release];
[imageView release];
[touchDown release];
[touchUpInside release];
[touchUpOutside release];
[self removeObserver:self forKeyPath:@"textLabel.text"];
}
@end