forked from Tencent/QMUI_iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQMUIEmptyView.m
More file actions
301 lines (245 loc) · 12.7 KB
/
Copy pathQMUIEmptyView.m
File metadata and controls
301 lines (245 loc) · 12.7 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//
// QMUIEmptyView.m
// qmui
//
// Created by 李凯 on 2016/10/9.
// Copyright © 2016年 QMUI Team. All rights reserved.
//
#import "QMUIEmptyView.h"
#import "QMUICore.h"
#import "UIControl+QMUI.h"
#import "NSParagraphStyle+QMUI.h"
@interface QMUIEmptyView ()
@property(nonatomic, strong) UIScrollView *scrollView; // 保证内容超出屏幕时也不至于直接被clip(比如横屏时)
@end
@implementation QMUIEmptyView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self didInitialized];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self didInitialized];
}
return self;
}
- (void)didInitialized {
// 系统默认会在view即将被add到window上时才设置这些值,这个时机有点晚了,因为我们可能在add到window之前就进行sizeThatFits计算或对view进行截图等操作,因此这里提前到init时就去做
QMUIEmptyView *appearance = [QMUIEmptyView appearance];
_imageViewInsets = appearance.imageViewInsets;
_loadingViewInsets = appearance.loadingViewInsets;
_textLabelInsets = appearance.textLabelInsets;
_detailTextLabelInsets = appearance.detailTextLabelInsets;
_actionButtonInsets = appearance.actionButtonInsets;
_verticalOffset = appearance.verticalOffset;
_textLabelFont = appearance.textLabelFont;
_detailTextLabelFont = appearance.detailTextLabelFont;
_actionButtonFont = appearance.actionButtonFont;
_textLabelTextColor = appearance.textLabelTextColor;
_detailTextLabelTextColor = appearance.detailTextLabelTextColor;
_actionButtonTitleColor = appearance.actionButtonTitleColor;
self.scrollView = [[UIScrollView alloc] init];
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.scrollsToTop = NO;
self.scrollView.contentInset = UIEdgeInsetsMake(0, 10, 0, 10); // 避免 label 直接撑满到屏幕两边,不好看
[self addSubview:self.scrollView];
_contentView = [[UIView alloc] init];
[self.scrollView addSubview:self.contentView];
_loadingView = (UIView<QMUIEmptyViewLoadingViewProtocol> *)[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
((UIActivityIndicatorView *)self.loadingView).hidesWhenStopped = NO; // 此控件是通过loadingView.hidden属性来控制显隐的,如果UIActivityIndicatorView的hidesWhenStopped属性设置为YES的话,则手动设置它的hidden属性就会失效,因此这里要置为NO
[self.contentView addSubview:self.loadingView];
_imageView = [[UIImageView alloc] init];
self.imageView.contentMode = UIViewContentModeCenter;
[self.contentView addSubview:self.imageView];
_textLabel = [[UILabel alloc] init];
self.textLabel.textAlignment = NSTextAlignmentCenter;
self.textLabel.numberOfLines = 0;
[self.contentView addSubview:self.textLabel];
_detailTextLabel = [[UILabel alloc] init];
self.detailTextLabel.textAlignment = NSTextAlignmentCenter;
self.detailTextLabel.numberOfLines = 0;
[self.contentView addSubview:self.detailTextLabel];
_actionButton = [[UIButton alloc] init];
self.actionButton.qmui_outsideEdge = UIEdgeInsetsMake(-20, -20, -20, -20);
[self.contentView addSubview:self.actionButton];
}
- (void)layoutSubviews {
[super layoutSubviews];
self.scrollView.frame = self.bounds;
CGSize contentViewSize = CGSizeFlatted([self sizeThatContentViewFits]);
self.contentView.frame = CGRectFlatMake(0, CGRectGetMidY(self.scrollView.bounds) - contentViewSize.height / 2 + self.verticalOffset, contentViewSize.width, contentViewSize.height);
self.scrollView.contentSize = CGSizeMake(fmaxf(CGRectGetWidth(self.scrollView.bounds) - UIEdgeInsetsGetHorizontalValue(self.scrollView.contentInset), contentViewSize.width), fmaxf(CGRectGetHeight(self.scrollView.bounds) - UIEdgeInsetsGetVerticalValue(self.scrollView.contentInset), CGRectGetMaxY(self.contentView.frame)));
CGFloat originY = 0;
if (!self.imageView.hidden) {
[self.imageView sizeToFit];
self.imageView.frame = CGRectSetXY(self.imageView.frame, CGRectGetMinXHorizontallyCenterInParentRect(self.contentView.bounds, self.imageView.frame) + self.imageViewInsets.left - self.imageViewInsets.right, originY + self.imageViewInsets.top);
originY = CGRectGetMaxY(self.imageView.frame) + self.imageViewInsets.bottom;
}
if (!self.loadingView.hidden) {
self.loadingView.frame = CGRectSetXY(self.loadingView.frame, CGRectGetMinXHorizontallyCenterInParentRect(self.contentView.bounds, self.loadingView.frame) + self.loadingViewInsets.left - self.loadingViewInsets.right, originY + self.loadingViewInsets.top);
originY = CGRectGetMaxY(self.loadingView.frame) + self.loadingViewInsets.bottom;
}
if (!self.textLabel.hidden) {
CGFloat labelWidth = CGRectGetWidth(self.contentView.bounds) - UIEdgeInsetsGetHorizontalValue(self.textLabelInsets);
CGSize labelSize = [self.textLabel sizeThatFits:CGSizeMake(labelWidth, CGFLOAT_MAX)];
self.textLabel.frame = CGRectFlatMake(self.textLabelInsets.left, originY + self.textLabelInsets.top, labelWidth, labelSize.height);
originY = CGRectGetMaxY(self.textLabel.frame) + self.textLabelInsets.bottom;
}
if (!self.detailTextLabel.hidden) {
CGFloat labelWidth = CGRectGetWidth(self.contentView.bounds) - UIEdgeInsetsGetHorizontalValue(self.detailTextLabelInsets);
CGSize labelSize = [self.detailTextLabel sizeThatFits:CGSizeMake(labelWidth, CGFLOAT_MAX)];
self.detailTextLabel.frame = CGRectFlatMake(self.detailTextLabelInsets.left, originY + self.detailTextLabelInsets.top, labelWidth, labelSize.height);
originY = CGRectGetMaxY(self.detailTextLabel.frame) + self.detailTextLabelInsets.bottom;
}
if (!self.actionButton.hidden) {
[self.actionButton sizeToFit];
self.actionButton.frame = CGRectSetXY(self.actionButton.frame, CGRectGetMinXHorizontallyCenterInParentRect(self.contentView.bounds, self.actionButton.frame) + self.actionButtonInsets.left - self.actionButtonInsets.right, originY + self.actionButtonInsets.top);
originY = CGRectGetMaxY(self.actionButton.frame) + self.actionButtonInsets.bottom;
}
}
- (CGSize)sizeThatContentViewFits {
CGFloat resultWidth = CGRectGetWidth(self.scrollView.bounds) - UIEdgeInsetsGetHorizontalValue(self.scrollView.contentInset);
CGFloat imageViewHeight = [self.imageView sizeThatFits:CGSizeMake(resultWidth, CGFLOAT_MAX)].height + UIEdgeInsetsGetVerticalValue(self.imageViewInsets);
CGFloat loadingViewHeight = CGRectGetHeight(self.loadingView.bounds) + UIEdgeInsetsGetVerticalValue(self.loadingViewInsets);
CGFloat textLabelHeight = [self.textLabel sizeThatFits:CGSizeMake(resultWidth, CGFLOAT_MAX)].height + UIEdgeInsetsGetVerticalValue(self.textLabelInsets);
CGFloat detailTextLabelHeight = [self.detailTextLabel sizeThatFits:CGSizeMake(resultWidth, CGFLOAT_MAX)].height + UIEdgeInsetsGetVerticalValue(self.detailTextLabelInsets);
CGFloat actionButtonHeight = [self.actionButton sizeThatFits:CGSizeMake(resultWidth, CGFLOAT_MAX)].height + UIEdgeInsetsGetVerticalValue(self.actionButtonInsets);
CGFloat resultHeight = 0;
if (!self.imageView.hidden) {
resultHeight += imageViewHeight;
}
if (!self.loadingView.hidden) {
resultHeight += loadingViewHeight;
}
if (!self.textLabel.hidden) {
resultHeight += textLabelHeight;
}
if (!self.detailTextLabel.hidden) {
resultHeight += detailTextLabelHeight;
}
if (!self.actionButton.hidden) {
resultHeight += actionButtonHeight;
}
return CGSizeMake(resultWidth, resultHeight);
}
- (void)updateDetailTextLabelWithText:(NSString *)text {
if (self.detailTextLabelFont && self.detailTextLabelTextColor && text) {
NSAttributedString *string = [[NSAttributedString alloc] initWithString:text attributes:@{
NSFontAttributeName: self.detailTextLabelFont,
NSForegroundColorAttributeName: self.detailTextLabelTextColor,
NSParagraphStyleAttributeName: [NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:self.detailTextLabelFont.pointSize + 10 lineBreakMode:NSLineBreakByWordWrapping textAlignment:NSTextAlignmentCenter]
}];
self.detailTextLabel.attributedText = string;
}
self.detailTextLabel.hidden = !text;
[self setNeedsLayout];
}
- (void)setLoadingView:(UIView<QMUIEmptyViewLoadingViewProtocol> *)loadingView {
if (self.loadingView != loadingView) {
[self.loadingView removeFromSuperview];
_loadingView = loadingView;
[self.contentView addSubview:loadingView];
}
[self setNeedsLayout];
}
- (void)setLoadingViewHidden:(BOOL)hidden {
self.loadingView.hidden = hidden;
if (!hidden && [self.loadingView respondsToSelector:@selector(startAnimating)]) {
[self.loadingView startAnimating];
}
[self setNeedsLayout];
}
- (void)setImage:(UIImage *)image {
self.imageView.image = image;
self.imageView.hidden = !image;
[self setNeedsLayout];
}
- (void)setTextLabelText:(NSString *)text {
self.textLabel.text = text;
self.textLabel.hidden = !text;
[self setNeedsLayout];
}
- (void)setDetailTextLabelText:(NSString *)text {
[self updateDetailTextLabelWithText:text];
}
- (void)setActionButtonTitle:(NSString *)title {
[self.actionButton setTitle:title forState:UIControlStateNormal];
self.actionButton.hidden = !title;
[self setNeedsLayout];
}
- (void)setImageViewInsets:(UIEdgeInsets)imageViewInsets {
_imageViewInsets = imageViewInsets;
[self setNeedsLayout];
}
- (void)setTextLabelInsets:(UIEdgeInsets)textLabelInsets {
_textLabelInsets = textLabelInsets;
[self setNeedsLayout];
}
- (void)setDetailTextLabelInsets:(UIEdgeInsets)detailTextLabelInsets {
_detailTextLabelInsets = detailTextLabelInsets;
[self setNeedsLayout];
}
- (void)setActionButtonInsets:(UIEdgeInsets)actionButtonInsets {
_actionButtonInsets = actionButtonInsets;
[self setNeedsLayout];
}
- (void)setVerticalOffset:(CGFloat)verticalOffset {
_verticalOffset = verticalOffset;
[self setNeedsLayout];
}
- (void)setTextLabelFont:(UIFont *)textLabelFont {
_textLabelFont = textLabelFont;
self.textLabel.font = textLabelFont;
[self setNeedsLayout];
}
- (void)setDetailTextLabelFont:(UIFont *)detailTextLabelFont {
_detailTextLabelFont = detailTextLabelFont;
[self updateDetailTextLabelWithText:self.detailTextLabel.text];
}
- (void)setActionButtonFont:(UIFont *)actionButtonFont {
_actionButtonFont = actionButtonFont;
self.actionButton.titleLabel.font = actionButtonFont;
[self setNeedsLayout];
}
- (void)setTextLabelTextColor:(UIColor *)textLabelTextColor {
_textLabelTextColor = textLabelTextColor;
self.textLabel.textColor = textLabelTextColor;
}
- (void)setDetailTextLabelTextColor:(UIColor *)detailTextLabelTextColor {
_detailTextLabelTextColor = detailTextLabelTextColor;
[self updateDetailTextLabelWithText:self.detailTextLabel.text];
}
- (void)setActionButtonTitleColor:(UIColor *)actionButtonTitleColor {
_actionButtonTitleColor = actionButtonTitleColor;
[self.actionButton setTitleColor:actionButtonTitleColor forState:UIControlStateNormal];
}
@end
@interface QMUIEmptyView (UIAppearance)
@end
@implementation QMUIEmptyView (UIAppearance)
+ (void)initialize {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self setDefaultAppearance];
});
}
+ (void)setDefaultAppearance {
QMUIEmptyView *appearance = [QMUIEmptyView appearance];
appearance.imageViewInsets = UIEdgeInsetsMake(0, 0, 36, 0);
appearance.loadingViewInsets = UIEdgeInsetsMake(0, 0, 36, 0);
appearance.textLabelInsets = UIEdgeInsetsMake(0, 0, 10, 0);
appearance.detailTextLabelInsets = UIEdgeInsetsMake(0, 0, 10, 0);
appearance.actionButtonInsets = UIEdgeInsetsZero;
appearance.verticalOffset = -30;
appearance.textLabelFont = UIFontMake(15);
appearance.detailTextLabelFont = UIFontMake(14);
appearance.actionButtonFont = UIFontMake(15);
appearance.textLabelTextColor = UIColorMake(93, 100, 110);
appearance.detailTextLabelTextColor = UIColorMake(133, 140, 150);
appearance.actionButtonTitleColor = ButtonTintColor;
}
@end