FLEXHierarchyTableViewCell.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // FLEXHierarchyTableViewCell.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 2014-05-02.
  6. // Copyright (c) 2014 Flipboard. 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. self.accessoryType = UITableViewCellAccessoryDetailButton;
  38. // Use a pattern-based color to simplify application of the checker pattern
  39. static UIColor *checkerPatternColor = nil;
  40. static dispatch_once_t once;
  41. dispatch_once(&once, ^{
  42. checkerPatternColor = [UIColor colorWithPatternImage:FLEXResources.checkerPattern];
  43. });
  44. self.backgroundColorCheckerPatternView = [UIView new];
  45. self.backgroundColorCheckerPatternView.clipsToBounds = YES;
  46. self.backgroundColorCheckerPatternView.layer.borderColor = FLEXColor.tertiaryBackgroundColor.CGColor;
  47. self.backgroundColorCheckerPatternView.layer.borderWidth = 2.f / UIScreen.mainScreen.scale;
  48. self.backgroundColorCheckerPatternView.backgroundColor = checkerPatternColor;
  49. [self.contentView addSubview:self.backgroundColorCheckerPatternView];
  50. self.viewBackgroundColorView = [UIView new];
  51. [self.backgroundColorCheckerPatternView addSubview:self.viewBackgroundColorView];
  52. }
  53. return self;
  54. }
  55. - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
  56. UIColor *originalColour = self.viewBackgroundColorView.backgroundColor;
  57. [super setHighlighted:highlighted animated:animated];
  58. // UITableViewCell changes all subviews in the contentView to backgroundColor = clearColor.
  59. // We want to preserve the hierarchy background color when highlighted.
  60. self.depthIndicatorView.backgroundColor = FLEXUtility.hierarchyIndentPatternColor;
  61. self.viewBackgroundColorView.backgroundColor = originalColour;
  62. }
  63. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  64. UIColor *originalColour = self.viewBackgroundColorView.backgroundColor;
  65. [super setSelected:selected animated:animated];
  66. // See setHighlighted above.
  67. self.depthIndicatorView.backgroundColor = FLEXUtility.hierarchyIndentPatternColor;
  68. self.viewBackgroundColorView.backgroundColor = originalColour;
  69. }
  70. - (void)layoutSubviews {
  71. [super layoutSubviews];
  72. const CGFloat kContentPadding = 6;
  73. const CGFloat kDepthIndicatorWidthMultiplier = 4;
  74. const CGFloat kViewColorIndicatorSize = 22;
  75. const CGRect bounds = self.contentView.bounds;
  76. const CGFloat centerY = CGRectGetMidY(bounds);
  77. const CGFloat textLabelCenterY = CGRectGetMidY(self.textLabel.frame);
  78. BOOL hideCheckerView = self.backgroundColorCheckerPatternView.hidden;
  79. CGFloat maxWidth = CGRectGetMaxX(bounds);
  80. maxWidth -= (hideCheckerView ? kContentPadding : (kViewColorIndicatorSize + kContentPadding * 2));
  81. CGRect depthIndicatorFrame = self.depthIndicatorView.frame = CGRectMake(
  82. kContentPadding, 0, self.viewDepth * kDepthIndicatorWidthMultiplier, CGRectGetHeight(bounds)
  83. );
  84. // Circle goes after depth, and its center Y = textLabel's center Y
  85. CGRect circleFrame = self.colorCircleImageView.frame;
  86. circleFrame.origin.x = CGRectGetMaxX(depthIndicatorFrame) + kContentPadding;
  87. circleFrame.origin.y = FLEXFloor(textLabelCenterY - CGRectGetHeight(circleFrame) / 2.f);
  88. self.colorCircleImageView.frame = circleFrame;
  89. // Text label goes after random color circle, width extends to the edge
  90. // of the contentView or to the padding before the color indicator view
  91. CGRect textLabelFrame = self.textLabel.frame;
  92. CGFloat textOriginX = CGRectGetMaxX(circleFrame) + kContentPadding;
  93. textLabelFrame.origin.x = textOriginX;
  94. textLabelFrame.size.width = maxWidth - textOriginX;
  95. self.textLabel.frame = textLabelFrame;
  96. // detailTextLabel leading edge lines up with the circle, and the
  97. // width extends to the same max X as the same max X as the textLabel
  98. CGRect detailTextLabelFrame = self.detailTextLabel.frame;
  99. CGFloat detailOriginX = circleFrame.origin.x;
  100. detailTextLabelFrame.origin.x = detailOriginX;
  101. detailTextLabelFrame.size.width = maxWidth - detailOriginX;
  102. self.detailTextLabel.frame = detailTextLabelFrame;
  103. // Checker pattern view starts after the padding after the max X of textLabel,
  104. // and is centered vertically within the entire contentView
  105. self.backgroundColorCheckerPatternView.frame = CGRectMake(
  106. CGRectGetMaxX(self.textLabel.frame) + kContentPadding,
  107. centerY - kViewColorIndicatorSize / 2.f,
  108. kViewColorIndicatorSize,
  109. kViewColorIndicatorSize
  110. );
  111. // Background color view fills it's superview
  112. self.viewBackgroundColorView.frame = self.backgroundColorCheckerPatternView.bounds;
  113. self.backgroundColorCheckerPatternView.layer.cornerRadius = kViewColorIndicatorSize / 2.f;
  114. }
  115. - (void)setRandomColorTag:(UIColor *)randomColorTag {
  116. if (![_randomColorTag isEqual:randomColorTag]) {
  117. _randomColorTag = randomColorTag;
  118. self.colorCircleImageView.image = [FLEXUtility circularImageWithColor:randomColorTag radius:6];
  119. }
  120. }
  121. - (void)setViewDepth:(NSInteger)viewDepth {
  122. if (_viewDepth != viewDepth) {
  123. _viewDepth = viewDepth;
  124. [self setNeedsLayout];
  125. }
  126. }
  127. - (UIColor *)indicatedViewColor {
  128. return self.viewBackgroundColorView.backgroundColor;
  129. }
  130. - (void)setIndicatedViewColor:(UIColor *)color {
  131. self.viewBackgroundColorView.backgroundColor = color;
  132. // Hide the checker pattern view if there is no background color
  133. self.backgroundColorCheckerPatternView.hidden = color == nil;
  134. [self setNeedsLayout];
  135. }
  136. @end