YYTextAsyncExample.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // YYTextAsyncExample.m
  3. // YYKitExample
  4. //
  5. // Created by ibireme on 15/9/3.
  6. // Copyright (c) 2015 ibireme. All rights reserved.
  7. //
  8. #import "YYTextAsyncExample.h"
  9. #import "YYKit.h"
  10. #import "YYFPSLabel.h"
  11. #define kCellHeight 34
  12. @interface YYTextAsyncExampleCell : UITableViewCell
  13. @property (nonatomic, assign) BOOL async;
  14. - (void)setAyncText:(NSAttributedString *)text;
  15. @end
  16. @implementation YYTextAsyncExampleCell {
  17. UILabel *_uiLabel;
  18. YYLabel *_yyLabel;
  19. }
  20. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  21. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  22. _uiLabel = [UILabel new];
  23. _uiLabel.font = [UIFont systemFontOfSize:8];
  24. _uiLabel.numberOfLines = 0;
  25. _uiLabel.size = CGSizeMake(kScreenWidth, kCellHeight);
  26. _yyLabel = [YYLabel new];
  27. _yyLabel.font = _uiLabel.font;
  28. _yyLabel.numberOfLines = _uiLabel.numberOfLines;
  29. _yyLabel.size = _uiLabel.size;
  30. _yyLabel.displaysAsynchronously = YES; /// enable async display
  31. _yyLabel.hidden = YES;
  32. [self.contentView addSubview:_uiLabel];
  33. [self.contentView addSubview:_yyLabel];
  34. return self;
  35. }
  36. - (void)setAsync:(BOOL)async {
  37. if (_async == async) return;
  38. _async = async;
  39. _uiLabel.hidden = async;
  40. _yyLabel.hidden = !async;
  41. }
  42. - (void)setAyncText:(id)text {
  43. if (_async) {
  44. _yyLabel.layer.contents = nil;
  45. _yyLabel.textLayout = text;
  46. } else {
  47. _uiLabel.attributedText = text;
  48. }
  49. }
  50. @end
  51. @interface YYTextAsyncExample () <UITableViewDelegate, UITableViewDataSource>
  52. @property (nonatomic, assign) BOOL async;
  53. @property (nonatomic, strong) NSArray *strings;
  54. @property (nonatomic, strong) NSArray *layouts;
  55. @property (nonatomic, strong) UITableView *tableView;
  56. @end
  57. @implementation YYTextAsyncExample
  58. - (void)viewDidLoad {
  59. [super viewDidLoad];
  60. self.tableView = [UITableView new];
  61. self.tableView.frame = self.view.bounds;
  62. self.tableView.delegate = self;
  63. self.tableView.dataSource = self;
  64. [self.tableView registerClass:[YYTextAsyncExampleCell class] forCellReuseIdentifier:@"id"];
  65. [self.view addSubview:self.tableView];
  66. NSMutableArray *strings = [NSMutableArray new];
  67. NSMutableArray *layouts = [NSMutableArray new];
  68. for (int i = 0; i < 300; i++) {
  69. NSString *str = [NSString stringWithFormat:@"%d Async Display Test ✺◟(∗❛ัᴗ❛ั∗)◞✺ ✺◟(∗❛ัᴗ❛ั∗)◞✺ 😀😖😐😣😡🚖🚌🚋🎊💖💗💛💙🏨🏦🏫 Async Display Test ✺◟(∗❛ัᴗ❛ั∗)◞✺ ✺◟(∗❛ัᴗ❛ั∗)◞✺ 😀😖😐😣😡🚖🚌🚋🎊💖💗💛💙🏨🏦🏫",i];
  70. NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];
  71. text.font = [UIFont systemFontOfSize:10];
  72. text.lineSpacing = 0;
  73. text.strokeWidth = @(-3);
  74. text.strokeColor = [UIColor redColor];
  75. text.lineHeightMultiple = 1;
  76. text.maximumLineHeight = 12;
  77. text.minimumLineHeight = 12;
  78. NSShadow *shadow = [NSShadow new];
  79. shadow.shadowBlurRadius = 1;
  80. shadow.shadowColor = [UIColor redColor];
  81. shadow.shadowOffset = CGSizeMake(0, 1);
  82. [strings addObject:text];
  83. // it better to do layout in background queue...
  84. YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(kScreenWidth, kCellHeight)];
  85. YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:text];
  86. [layouts addObject:layout];
  87. }
  88. self.strings = strings;
  89. self.layouts = layouts;
  90. UIView *toolbar;
  91. if ([UIVisualEffectView class]) {
  92. toolbar = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
  93. } else {
  94. toolbar = [UIToolbar new];
  95. }
  96. toolbar.size = CGSizeMake(kScreenWidth, 40);
  97. toolbar.top = kiOS7Later ? 64 : 0;
  98. [self.view addSubview:toolbar];
  99. YYFPSLabel *fps = [YYFPSLabel new];
  100. fps.centerY = toolbar.height / 2;
  101. fps.left = 5;
  102. [toolbar addSubview:fps];
  103. UILabel *label = [UILabel new];
  104. label.backgroundColor = [UIColor clearColor];
  105. label.text = @"UILabel/YYLabel(Async): ";
  106. label.font = [UIFont systemFontOfSize:14];
  107. [label sizeToFit];
  108. label.centerY = toolbar.height / 2;
  109. label.left = fps.right + 10;
  110. [toolbar addSubview:label];
  111. UISwitch *switcher = [UISwitch new];
  112. [switcher sizeToFit];
  113. switcher.centerY = toolbar.height / 2;
  114. switcher.left = label.right + (kiOS7Later ? 10 : -10);
  115. switcher.layer.transformScale = 0.7;
  116. @weakify(self);
  117. [switcher addBlockForControlEvents:UIControlEventValueChanged block:^(UISwitch *switcher) {
  118. @strongify(self);
  119. if (!self) return;
  120. [self setAsync:switcher.isOn];
  121. }];
  122. [toolbar addSubview:switcher];
  123. }
  124. - (void)setAsync:(BOOL)async {
  125. _async = async;
  126. [self.tableView.visibleCells enumerateObjectsUsingBlock:^(YYTextAsyncExampleCell *cell, NSUInteger idx, BOOL *stop) {
  127. cell.async = async;
  128. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  129. if (_async) {
  130. [cell setAyncText:_layouts[indexPath.row]];
  131. } else {
  132. [cell setAyncText:_strings[indexPath.row]];
  133. }
  134. }];
  135. }
  136. #pragma mark - Table view data source
  137. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  138. return _strings.count;
  139. }
  140. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  141. return kCellHeight;
  142. }
  143. - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
  144. return NO;
  145. }
  146. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  147. YYTextAsyncExampleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"id" forIndexPath:indexPath];
  148. cell.async = _async;
  149. if (_async) {
  150. [cell setAyncText:_layouts[indexPath.row]];
  151. } else {
  152. [cell setAyncText:_strings[indexPath.row]];
  153. }
  154. return cell;
  155. }
  156. @end