-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathJDStatusBarView.m
executable file
·99 lines (82 loc) · 2.94 KB
/
JDStatusBarView.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
//
// JDStatusBarView.m
// JDStatusBarNotificationExample
//
// Created by Markus on 04.12.13.
// Copyright (c) 2013 Markus. All rights reserved.
//
#import "JDStatusBarView.h"
#import "JDStatusBarLayoutMarginHelper.h"
@interface JDStatusBarView ()
@property (nonatomic, strong) UILabel *textLabel;
@property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;
@end
@implementation JDStatusBarView
#pragma mark dynamic getter
- (UILabel *)textLabel;
{
if (_textLabel == nil) {
_textLabel = [[UILabel alloc] init];
_textLabel.backgroundColor = [UIColor clearColor];
_textLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
_textLabel.textAlignment = NSTextAlignmentCenter;
_textLabel.adjustsFontSizeToFitWidth = YES;
_textLabel.clipsToBounds = YES;
[self addSubview:_textLabel];
}
return _textLabel;
}
- (UIActivityIndicatorView *)activityIndicatorView;
{
if (_activityIndicatorView == nil) {
_activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
_activityIndicatorView.transform = CGAffineTransformMakeScale(0.7, 0.7);
[self addSubview:_activityIndicatorView];
}
return _activityIndicatorView;
}
#pragma mark setter
- (void)setTextVerticalPositionAdjustment:(CGFloat)textVerticalPositionAdjustment;
{
_textVerticalPositionAdjustment = textVerticalPositionAdjustment;
[self setNeedsLayout];
}
#pragma mark layout
- (void)layoutSubviews;
{
[super layoutSubviews];
// label
CGFloat topLayoutMargin = JDStatusBarRootVCLayoutMargin().top;
self.textLabel.frame = CGRectMake(0,
self.textVerticalPositionAdjustment + topLayoutMargin + 1,
self.bounds.size.width,
self.bounds.size.height - topLayoutMargin - 1);
// activity indicator
if (_activityIndicatorView ) {
CGSize textSize = [self currentTextSize];
CGRect indicatorFrame = _activityIndicatorView.frame;
indicatorFrame.origin.x = round((self.bounds.size.width - textSize.width)/2.0) - indicatorFrame.size.width - 8.0;
indicatorFrame.origin.y = ceil(1+(self.bounds.size.height - indicatorFrame.size.height + topLayoutMargin)/2.0);
_activityIndicatorView.frame = indicatorFrame;
}
}
- (CGSize)currentTextSize;
{
CGSize textSize = CGSizeZero;
// use new sizeWithAttributes: if possible
SEL selector = NSSelectorFromString(@"sizeWithAttributes:");
if ([self.textLabel.text respondsToSelector:selector]) {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
NSDictionary *attributes = @{NSFontAttributeName:self.textLabel.font};
textSize = [self.textLabel.text sizeWithAttributes:attributes];
#endif
}
// otherwise use old sizeWithFont:
else {
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 70000 // only when deployment target is < ios7
textSize = [self.textLabel.text sizeWithFont:self.textLabel.font];
#endif
}
return textSize;
}
@end