-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathYYTextAsyncExample.m
More file actions
204 lines (166 loc) · 6.48 KB
/
Copy pathYYTextAsyncExample.m
File metadata and controls
204 lines (166 loc) · 6.48 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
//
// YYTextAsyncExample.m
// YYKitExample
//
// Created by ibireme on 15/9/3.
// Copyright (c) 2015 ibireme. All rights reserved.
//
#import "YYTextAsyncExample.h"
#import "YYKit.h"
#import "YYFPSLabel.h"
#define kCellHeight 34
@interface YYTextAsyncExampleCell : UITableViewCell
@property (nonatomic, assign) BOOL async;
- (void)setAyncText:(NSAttributedString *)text;
@end
@implementation YYTextAsyncExampleCell {
UILabel *_uiLabel;
YYLabel *_yyLabel;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
_uiLabel = [UILabel new];
_uiLabel.font = [UIFont systemFontOfSize:8];
_uiLabel.numberOfLines = 0;
_uiLabel.size = CGSizeMake(kScreenWidth, kCellHeight);
_yyLabel = [YYLabel new];
_yyLabel.font = _uiLabel.font;
_yyLabel.numberOfLines = _uiLabel.numberOfLines;
_yyLabel.size = _uiLabel.size;
_yyLabel.displaysAsynchronously = YES; /// enable async display
_yyLabel.hidden = YES;
[self.contentView addSubview:_uiLabel];
[self.contentView addSubview:_yyLabel];
return self;
}
- (void)setAsync:(BOOL)async {
if (_async == async) return;
_async = async;
_uiLabel.hidden = async;
_yyLabel.hidden = !async;
}
- (void)setAyncText:(id)text {
if (_async) {
_yyLabel.layer.contents = nil;
_yyLabel.textLayout = text;
} else {
_uiLabel.attributedText = text;
}
}
@end
@interface YYTextAsyncExample () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, assign) BOOL async;
@property (nonatomic, strong) NSArray *strings;
@property (nonatomic, strong) NSArray *layouts;
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation YYTextAsyncExample
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView = [UITableView new];
self.tableView.frame = self.view.bounds;
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView registerClass:[YYTextAsyncExampleCell class] forCellReuseIdentifier:@"id"];
[self.view addSubview:self.tableView];
NSMutableArray *strings = [NSMutableArray new];
NSMutableArray *layouts = [NSMutableArray new];
for (int i = 0; i < 300; i++) {
NSString *str = [NSString stringWithFormat:@"%d Async Display Test ✺◟(∗❛ัᴗ❛ั∗)◞✺ ✺◟(∗❛ัᴗ❛ั∗)◞✺ 😀😖😐😣😡🚖🚌🚋🎊💖💗💛💙🏨🏦🏫 Async Display Test ✺◟(∗❛ัᴗ❛ั∗)◞✺ ✺◟(∗❛ัᴗ❛ั∗)◞✺ 😀😖😐😣😡🚖🚌🚋🎊💖💗💛💙🏨🏦🏫",i];
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];
text.font = [UIFont systemFontOfSize:10];
text.lineSpacing = 0;
text.strokeWidth = @(-3);
text.strokeColor = [UIColor redColor];
text.lineHeightMultiple = 1;
text.maximumLineHeight = 12;
text.minimumLineHeight = 12;
NSShadow *shadow = [NSShadow new];
shadow.shadowBlurRadius = 1;
shadow.shadowColor = [UIColor redColor];
shadow.shadowOffset = CGSizeMake(0, 1);
[strings addObject:text];
// it better to do layout in background queue...
YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(kScreenWidth, kCellHeight)];
YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:text];
[layouts addObject:layout];
}
self.strings = strings;
self.layouts = layouts;
// UIView *toolbar;
// if ([UIVisualEffectView class]) {
// toolbar = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
// } else {
// toolbar = [UIToolbar new];
// }
//
UIView *toolbar ;
UIVisualEffectView *toolbarEffectView;
if ([UIVisualEffectView class]) {
toolbarEffectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
toolbar = [UIView new];
[toolbarEffectView.contentView addSubview:toolbar];
} else {
toolbar = [UIToolbar new];
}
toolbar.size = CGSizeMake(kScreenWidth, 40);
toolbar.top = kiOS7Later ? 64 : 0;
[self.view addSubview:toolbar];
YYFPSLabel *fps = [YYFPSLabel new];
fps.centerY = toolbar.height / 2;
fps.left = 5;
[toolbar addSubview:fps];
UILabel *label = [UILabel new];
label.backgroundColor = [UIColor clearColor];
label.text = @"UILabel/YYLabel(Async): ";
label.font = [UIFont systemFontOfSize:14];
[label sizeToFit];
label.centerY = toolbar.height / 2;
label.left = fps.right + 10;
[toolbar addSubview:label];
UISwitch *switcher = [UISwitch new];
[switcher sizeToFit];
switcher.centerY = toolbar.height / 2;
switcher.left = label.right + (kiOS7Later ? 10 : -10);
switcher.layer.transformScale = 0.7;
@weakify(self);
[switcher addBlockForControlEvents:UIControlEventValueChanged block:^(UISwitch *switcher) {
@strongify(self);
if (!self) return;
[self setAsync:switcher.isOn];
}];
[toolbar addSubview:switcher];
}
- (void)setAsync:(BOOL)async {
_async = async;
[self.tableView.visibleCells enumerateObjectsUsingBlock:^(YYTextAsyncExampleCell *cell, NSUInteger idx, BOOL *stop) {
cell.async = async;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
if (_async) {
[cell setAyncText:_layouts[indexPath.row]];
} else {
[cell setAyncText:_strings[indexPath.row]];
}
}];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _strings.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return kCellHeight;
}
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
YYTextAsyncExampleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"id" forIndexPath:indexPath];
cell.async = _async;
if (_async) {
[cell setAyncText:_layouts[indexPath.row]];
} else {
[cell setAyncText:_strings[indexPath.row]];
}
return cell;
}
@end