LKS_PerspectiveHierarchyCell.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #ifdef SHOULD_COMPILE_LOOKIN_SERVER
  2. //
  3. // LKS_PerspectiveHierarchyCell.m
  4. // LookinServer
  5. //
  6. // Created by Li Kai on 2018/12/24.
  7. // https://lookin.work
  8. //
  9. #import "LKS_PerspectiveHierarchyCell.h"
  10. #import "LookinDisplayItem.h"
  11. #import "LookinIvarTrace.h"
  12. #import "LookinServerDefines.h"
  13. @interface LKS_PerspectiveHierarchyCell ()
  14. @property(nonatomic, strong) UILabel *titleLabel;
  15. @property(nonatomic, strong) UILabel *subtitleLabel;
  16. @property(nonatomic, strong) CALayer *strikethroughLayer;
  17. @property(nonatomic, assign) CGFloat cachedContentWidth;
  18. @end
  19. @implementation LKS_PerspectiveHierarchyCell {
  20. CGFloat _horInset;
  21. CGFloat _indicatorWidth;
  22. CGFloat _iconImageMarginLeft;
  23. CGFloat _indentUnitWidth;
  24. CGFloat _titleLeft;
  25. CGFloat _subtitleLeft;
  26. }
  27. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  28. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  29. _horInset = 10;
  30. _indicatorWidth = 15;
  31. _iconImageMarginLeft = 5;
  32. _indentUnitWidth = 10;
  33. _titleLeft = 6;
  34. _subtitleLeft = 10;
  35. _indicatorButton = [UIButton new];
  36. [self.contentView addSubview:self.indicatorButton];
  37. self.titleLabel = [UILabel new];
  38. self.titleLabel.font = [UIFont systemFontOfSize:12];
  39. [self.contentView addSubview:self.titleLabel];
  40. self.subtitleLabel = [UILabel new];
  41. self.subtitleLabel.font = [UIFont systemFontOfSize:11];
  42. [self.contentView addSubview:self.subtitleLabel];
  43. }
  44. return self;
  45. }
  46. - (void)layoutSubviews {
  47. [super layoutSubviews];
  48. self.indicatorButton.frame = ({
  49. CGFloat x = self.displayItem.indentLevel * _indentUnitWidth + _horInset;
  50. CGRectMake(x, 0, _indicatorWidth, self.bounds.size.height);
  51. });
  52. self.titleLabel.frame = ({
  53. CGFloat width = self.titleLabel.lks_bestWidth;
  54. CGRectMake(CGRectGetMaxX(self.indicatorButton.frame) + _titleLeft, 0, width, self.bounds.size.height);
  55. });
  56. CGFloat labelMaxX = CGRectGetMaxX(self.titleLabel.frame);
  57. if (!self.subtitleLabel.hidden) {
  58. self.subtitleLabel.frame = ({
  59. CGFloat width = self.subtitleLabel.lks_bestWidth;
  60. CGRectMake(CGRectGetMaxX(self.titleLabel.frame) + _subtitleLeft, 0, width, self.bounds.size.height);
  61. });
  62. labelMaxX = CGRectGetMaxX(self.subtitleLabel.frame);
  63. }
  64. if (self.strikethroughLayer && !self.strikethroughLayer.hidden) {
  65. self.strikethroughLayer.frame = ({
  66. CGFloat x = CGRectGetMinX(self.titleLabel.frame) - 2;
  67. CGFloat maxX = self.subtitleLabel.hidden ? (CGRectGetMaxX(self.titleLabel.frame) + 2) : (CGRectGetMaxX(self.subtitleLabel.frame) + 2);
  68. CGFloat width = maxX - x;
  69. CGRectMake(x, CGRectGetMidY(self.bounds), width, 1);
  70. });
  71. }
  72. }
  73. - (CGSize)sizeThatFits:(CGSize)size {
  74. size.width = self.cachedContentWidth;
  75. return size;
  76. }
  77. - (void)setDisplayItem:(LookinDisplayItem *)displayItem {
  78. _displayItem = displayItem;
  79. [self reRender];
  80. }
  81. - (void)reRender {
  82. // text
  83. self.titleLabel.text = self.displayItem.title;
  84. // subtitle
  85. self.subtitleLabel.text = self.displayItem.subtitle;
  86. self.subtitleLabel.hidden = (self.displayItem.subtitle.length == 0);
  87. // select
  88. if (self.displayItem.isSelected) {
  89. self.backgroundColor = LookinColorRGBAMake(172, 177, 191, .4);
  90. self.subtitleLabel.textColor = [UIColor whiteColor];
  91. } else {
  92. self.backgroundColor = [UIColor clearColor];
  93. self.subtitleLabel.textColor = LookinColorMake(133, 140, 150);
  94. }
  95. // icon
  96. if (!self.displayItem.isExpandable) {
  97. self.indicatorButton.hidden = YES;
  98. } else if (self.displayItem.isExpanded) {
  99. [self.indicatorButton setImage:[self _arrowDownImage] forState:UIControlStateNormal];
  100. self.indicatorButton.hidden = NO;
  101. } else {
  102. [self.indicatorButton setImage:[self _arrowRightImage] forState:UIControlStateNormal];
  103. self.indicatorButton.hidden = NO;
  104. }
  105. // strike
  106. if (self.displayItem.inNoPreviewHierarchy) {
  107. if (!self.strikethroughLayer) {
  108. self.strikethroughLayer = [CALayer layer];
  109. [self.strikethroughLayer lookin_removeImplicitAnimations];
  110. self.strikethroughLayer.backgroundColor = LookinColorRGBAMake(255, 255, 255, .3).CGColor;
  111. [self.layer addSublayer:self.strikethroughLayer];
  112. }
  113. self.strikethroughLayer.hidden = NO;
  114. if (self.displayItem.isSelected) {
  115. self.titleLabel.textColor = [UIColor whiteColor];
  116. } else {
  117. self.titleLabel.textColor = LookinColorMake(113, 120, 130);
  118. }
  119. } else {
  120. self.strikethroughLayer.hidden = YES;
  121. self.titleLabel.textColor = [UIColor whiteColor];
  122. }
  123. [self setNeedsLayout];
  124. self.cachedContentWidth = ({
  125. CGFloat width = 0;
  126. width = _horInset + self.displayItem.indentLevel * _indentUnitWidth + _indicatorWidth + _iconImageMarginLeft + _titleLeft + self.titleLabel.lks_bestWidth + _horInset;
  127. if (!self.subtitleLabel.hidden) {
  128. width += self.subtitleLabel.lks_bestWidth + _subtitleLeft;
  129. }
  130. width;
  131. });
  132. }
  133. - (UIImage *)_arrowRightImage {
  134. static UIImage *image = nil;
  135. if (image) {
  136. return image;
  137. }
  138. CGFloat width = 10;
  139. UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, width), NO, 0);
  140. CGContextRef context = UIGraphicsGetCurrentContext();
  141. UIBezierPath *path = [UIBezierPath bezierPath];
  142. [path moveToPoint:CGPointMake(0, 0)];
  143. [path addLineToPoint:CGPointMake(width - 2, width / 2.0)];
  144. [path addLineToPoint:CGPointMake(0, width)];
  145. [path closePath];
  146. CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
  147. [path fill];
  148. image = UIGraphicsGetImageFromCurrentImageContext();
  149. UIGraphicsEndImageContext();
  150. return image;
  151. }
  152. - (UIImage *)_arrowDownImage {
  153. static UIImage *image = nil;
  154. if (image) {
  155. return image;
  156. }
  157. CGFloat width = 10;
  158. UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, width), NO, 0);
  159. CGContextRef context = UIGraphicsGetCurrentContext();
  160. UIBezierPath *path = [UIBezierPath bezierPath];
  161. [path moveToPoint:CGPointMake(0, 0)];
  162. [path addLineToPoint:CGPointMake(width, 0)];
  163. [path addLineToPoint:CGPointMake(width / 2.0, width - 2)];
  164. [path closePath];
  165. CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
  166. [path fill];
  167. image = UIGraphicsGetImageFromCurrentImageContext();
  168. UIGraphicsEndImageContext();
  169. return image;
  170. }
  171. @end
  172. #endif /* SHOULD_COMPILE_LOOKIN_SERVER */