This repository has been archived by the owner on Feb 16, 2019. It is now read-only.
/
SSLoadingView.m
104 lines (77 loc) · 2.77 KB
/
SSLoadingView.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
104
//
// SSLoadingView.m
// SSToolkit
//
// Created by Sam Soffes on 7/8/09.
// Copyright 2009-2011 Sam Soffes. All rights reserved.
//
#import "SSLoadingView.h"
static CGFloat interiorPadding = 20.0f;
static CGFloat indicatorSize = 20.0f;
static CGFloat indicatorRightMargin = 8.0f;
@interface SSLoadingView ()
- (void)_initialize;
@end
@implementation SSLoadingView
#pragma mark - Accessors
@synthesize textLabel = _textLabel;
@synthesize activityIndicatorView = _activityIndicatorView;
#pragma mark - UIView
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self _initialize];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self _initialize];
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGRect frame = self.frame;
// Calculate sizes
CGSize maxSize = CGSizeMake(frame.size.width - (interiorPadding * 2.0f) - indicatorSize - indicatorRightMargin,
indicatorSize);
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_6_0
CGSize textSize = [_textLabel.text sizeWithFont:_textLabel.font constrainedToSize:maxSize
lineBreakMode:NSLineBreakByWordWrapping];
#else
CGSize textSize = [_textLabel.text sizeWithFont:_textLabel.font constrainedToSize:maxSize
lineBreakMode:UILineBreakModeWordWrap];
#endif
// Calculate position
CGFloat totalWidth = textSize.width + indicatorSize + indicatorRightMargin;
NSInteger y = (NSInteger)((frame.size.height / 2.0f) - (indicatorSize / 2.0f));
// Position the indicator
_activityIndicatorView.frame = CGRectMake((NSInteger)((frame.size.width - totalWidth) / 2.0f), y, indicatorSize,
indicatorSize);
// Calculate text position
CGRect textRect = CGRectMake(_activityIndicatorView.frame.origin.x + indicatorSize + indicatorRightMargin, y,
textSize.width, textSize.height);
// Draw text
[_textLabel drawTextInRect:textRect];
}
#pragma mark - Private
- (void)_initialize {
// View defaults
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.backgroundColor = [UIColor whiteColor];
self.opaque = YES;
self.contentMode = UIViewContentModeRedraw;
// Setup label
_textLabel = [[UILabel alloc] initWithFrame:CGRectZero];
// Setup the indicator
_activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_activityIndicatorView.hidesWhenStopped = NO;
[_activityIndicatorView startAnimating];
[self addSubview:_activityIndicatorView];
// Defaults
_textLabel.text = @"Loading...";
_textLabel.font = [UIFont systemFontOfSize:16.0f];
_textLabel.textColor = [UIColor darkGrayColor];
_textLabel.shadowColor = [UIColor whiteColor];
_textLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
}
@end