FLEXHierarchyTableViewCell.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // FLEXHierarchyTableViewCell.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 2014-05-02.
  6. // Copyright (c) 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXHierarchyTableViewCell.h"
  9. #import "FLEXUtility.h"
  10. #import "FLEXResources.h"
  11. #import "FLEXColor.h"
  12. @interface FLEXHierarchyTableViewCell ()
  13. /// Indicates how deep the view is in the hierarchy
  14. @property (nonatomic) UIView *depthIndicatorView;
  15. /// Holds the color that visually distinguishes views from one another
  16. @property (nonatomic) UIImageView *colorCircleImageView;
  17. /// A checker-patterned view, used to help show the color of a view, like a photoshop canvas
  18. @property (nonatomic) UIView *backgroundColorCheckerPatternView;
  19. /// The subview of the checker pattern view which holds the actual color of the view
  20. @property (nonatomic) UIView *viewBackgroundColorView;
  21. @end
  22. @implementation FLEXHierarchyTableViewCell
  23. - (id)initWithReuseIdentifier:(NSString *)reuseIdentifier {
  24. return [self initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
  25. }
  26. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  27. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  28. if (self) {
  29. self.depthIndicatorView = [UIView new];
  30. self.depthIndicatorView.backgroundColor = FLEXUtility.hierarchyIndentPatternColor;
  31. [self.contentView addSubview:self.depthIndicatorView];
  32. UIImage *defaultCircleImage = [FLEXUtility circularImageWithColor:UIColor.blackColor radius:5];
  33. self.colorCircleImageView = [[UIImageView alloc] initWithImage:defaultCircleImage];
  34. [self.contentView addSubview:self.colorCircleImageView];
  35. self.textLabel.font = UIFont.flex_defaultTableCellFont;
  36. self.detailTextLabel.font = UIFont.flex_defaultTableCellFont;
  37. #if !TARGET_OS_TV
  38. self.accessoryType = UITableViewCellAccessoryDetailButton;
  39. #else
  40. self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  41. #endif
  42. // Use a pattern-based color to simplify application of the checker pattern
  43. static UIColor *checkerPatternColor = nil;
  44. static dispatch_once_t once;
  45. dispatch_once(&once, ^{
  46. checkerPatternColor = [UIColor colorWithPatternImage:FLEXResources.checkerPattern];
  47. });
  48. self.backgroundColorCheckerPatternView = [UIView new];
  49. self.backgroundColorCheckerPatternView.clipsToBounds = YES;
  50. self.backgroundColorCheckerPatternView.layer.borderColor = FLEXColor.tertiaryBackgroundColor.CGColor;
  51. self.backgroundColorCheckerPatternView.layer.borderWidth = 2.f / UIScreen.mainScreen.scale;
  52. self.backgroundColorCheckerPatternView.backgroundColor = checkerPatternColor;
  53. [self.contentView addSubview:self.backgroundColorCheckerPatternView];
  54. self.viewBackgroundColorView = [UIView new];
  55. [self.backgroundColorCheckerPatternView addSubview:self.viewBackgroundColorView];
  56. }
  57. return self;
  58. }
  59. - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
  60. UIColor *originalColour = self.viewBackgroundColorView.backgroundColor;
  61. [super setHighlighted:highlighted animated:animated];
  62. // UITableViewCell changes all subviews in the contentView to backgroundColor = clearColor.
  63. // We want to preserve the hierarchy background color when highlighted.
  64. self.depthIndicatorView.backgroundColor = FLEXUtility.hierarchyIndentPatternColor;
  65. self.viewBackgroundColorView.backgroundColor = originalColour;
  66. }
  67. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  68. UIColor *originalColour = self.viewBackgroundColorView.backgroundColor;
  69. [super setSelected:selected animated:animated];
  70. // See setHighlighted above.
  71. self.depthIndicatorView.backgroundColor = FLEXUtility.hierarchyIndentPatternColor;
  72. self.viewBackgroundColorView.backgroundColor = originalColour;
  73. }
  74. - (void)layoutSubviews {
  75. [super layoutSubviews];
  76. const CGFloat kContentPadding = 6;
  77. const CGFloat kDepthIndicatorWidthMultiplier = 4;
  78. const CGFloat kViewColorIndicatorSize = 22;
  79. const CGRect bounds = self.contentView.bounds;
  80. const CGFloat centerY = CGRectGetMidY(bounds);
  81. const CGFloat textLabelCenterY = CGRectGetMidY(self.textLabel.frame);
  82. BOOL hideCheckerView = self.backgroundColorCheckerPatternView.hidden;
  83. CGFloat maxWidth = CGRectGetMaxX(bounds);
  84. maxWidth -= (hideCheckerView ? kContentPadding : (kViewColorIndicatorSize + kContentPadding * 2));
  85. CGRect depthIndicatorFrame = self.depthIndicatorView.frame = CGRectMake(
  86. kContentPadding, 0, self.viewDepth * kDepthIndicatorWidthMultiplier, CGRectGetHeight(bounds)
  87. );
  88. // Circle goes after depth, and its center Y = textLabel's center Y
  89. CGRect circleFrame = self.colorCircleImageView.frame;
  90. circleFrame.origin.x = CGRectGetMaxX(depthIndicatorFrame) + kContentPadding;
  91. circleFrame.origin.y = FLEXFloor(textLabelCenterY - CGRectGetHeight(circleFrame) / 2.f);
  92. self.colorCircleImageView.frame = circleFrame;
  93. // Text label goes after random color circle, width extends to the edge
  94. // of the contentView or to the padding before the color indicator view
  95. CGRect textLabelFrame = self.textLabel.frame;
  96. CGFloat textOriginX = CGRectGetMaxX(circleFrame) + kContentPadding;
  97. textLabelFrame.origin.x = textOriginX;
  98. textLabelFrame.size.width = maxWidth - textOriginX;
  99. self.textLabel.frame = textLabelFrame;
  100. // detailTextLabel leading edge lines up with the circle, and the
  101. // width extends to the same max X as the same max X as the textLabel
  102. CGRect detailTextLabelFrame = self.detailTextLabel.frame;
  103. CGFloat detailOriginX = circleFrame.origin.x;
  104. detailTextLabelFrame.origin.x = detailOriginX;
  105. detailTextLabelFrame.size.width = maxWidth - detailOriginX;
  106. self.detailTextLabel.frame = detailTextLabelFrame;
  107. // Checker pattern view starts after the padding after the max X of textLabel,
  108. // and is centered vertically within the entire contentView
  109. self.backgroundColorCheckerPatternView.frame = CGRectMake(
  110. CGRectGetMaxX(self.textLabel.frame) + kContentPadding,
  111. centerY - kViewColorIndicatorSize / 2.f,
  112. kViewColorIndicatorSize,
  113. kViewColorIndicatorSize
  114. );
  115. // Background color view fills it's superview
  116. self.viewBackgroundColorView.frame = self.backgroundColorCheckerPatternView.bounds;
  117. self.backgroundColorCheckerPatternView.layer.cornerRadius = kViewColorIndicatorSize / 2.f;
  118. }
  119. - (void)setRandomColorTag:(UIColor *)randomColorTag {
  120. if (![_randomColorTag isEqual:randomColorTag]) {
  121. _randomColorTag = randomColorTag;
  122. self.colorCircleImageView.image = [FLEXUtility circularImageWithColor:randomColorTag radius:6];
  123. }
  124. }
  125. - (void)setViewDepth:(NSInteger)viewDepth {
  126. if (_viewDepth != viewDepth) {
  127. _viewDepth = viewDepth;
  128. [self setNeedsLayout];
  129. }
  130. }
  131. - (UIColor *)indicatedViewColor {
  132. return self.viewBackgroundColorView.backgroundColor;
  133. }
  134. - (void)setIndicatedViewColor:(UIColor *)color {
  135. self.viewBackgroundColorView.backgroundColor = color;
  136. // Hide the checker pattern view if there is no background color
  137. self.backgroundColorCheckerPatternView.hidden = color == nil;
  138. [self setNeedsLayout];
  139. }
  140. @end