-
Notifications
You must be signed in to change notification settings - Fork 1
/
TLLoadingView.m
82 lines (62 loc) · 2.69 KB
/
TLLoadingView.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
//
// TLLoadingView.m
// TLCommon
//
// Created by Joshua Bleecher Snyder on 10/18/09.
//
#import "TLLoadingView.h"
#import "UIActivityIndicatorView_TLCommon.h"
#import "CGGeometry_TLCommon.h"
#define kDefaultLoadingText NSLocalizedString(@"Loading...", @"Default text of loading view")
#pragma mark -
@interface TLLoadingView ()
@property(nonatomic, retain, readwrite) UILabel *label;
@property(nonatomic, retain, readwrite) UIActivityIndicatorView *spinner;
@end
#pragma mark -
@implementation TLLoadingView
@synthesize label;
@synthesize spinner;
- (id)initWithFrame:(CGRect)frame {
if(self = [super initWithFrame:frame]) {
self.label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
self.label.text = kDefaultLoadingText;
self.spinner = [UIActivityIndicatorView animatingActivityIndicatorViewWithStyle:UIActivityIndicatorViewStyleGray];
[self addSubview:self.label];
[self addSubview:self.spinner];
[self setNeedsLayout];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat viewHeight = self.bounds.size.height;
CGFloat viewWidth = self.bounds.size.width;
CGSize spinnerSize = self.spinner.bounds.size;
CGFloat spinnerToLabelPadding = spinnerSize.width / 2.0f;
CGFloat horizontalPadding = spinnerToLabelPadding; // required distance between left/right edge of view and spinner/label
CGFloat maxLabelWidth = viewWidth - spinnerSize.width - horizontalPadding * 2.0f - spinnerToLabelPadding;
CGSize renderedLabelSize = [self.label.text sizeWithFont:self.label.font
constrainedToSize:CGSizeMake(maxLabelWidth, viewHeight)
lineBreakMode:self.label.lineBreakMode];
CGFloat spinnerPlusLabelWidth = spinnerSize.width + spinnerToLabelPadding + renderedLabelSize.width;
CGFloat spinnerXOffset = OffsetToCenterFloatInFloat(spinnerPlusLabelWidth, viewWidth);
CGRect spinnerFrame = CGRectWithXYAndSize(spinnerXOffset,
OffsetToCenterFloatInFloat(spinnerSize.height, viewHeight),
spinnerSize);
spinnerFrame = CGRectFlooredToNearestPixel(spinnerFrame);
self.spinner.frame = spinnerFrame;
CGRect labelFrame = CGRectWithXYAndSize(spinnerXOffset + spinnerSize.width + spinnerToLabelPadding,
OffsetToCenterFloatInFloat(renderedLabelSize.height, viewHeight),
renderedLabelSize);
labelFrame = CGRectFlooredToNearestPixel(labelFrame);
self.label.frame = labelFrame;
}
- (void)dealloc {
[label release];
label = nil;
[spinner release];
spinner = nil;
[super dealloc];
}
@end