You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
5.1 KiB

  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. @interface FLEXHierarchyTableViewCell ()
  11. @property (nonatomic, strong) UIView *depthIndicatorView;
  12. @property (nonatomic, strong) UIImageView *colorCircleImageView;
  13. @end
  14. @implementation FLEXHierarchyTableViewCell
  15. - (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
  16. {
  17. return [self initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
  18. }
  19. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  20. {
  21. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  22. if (self) {
  23. self.depthIndicatorView = [[UIView alloc] init];
  24. self.depthIndicatorView.backgroundColor = [FLEXUtility hierarchyIndentPatternColor];
  25. [self.contentView addSubview:self.depthIndicatorView];
  26. UIImage *defaultCircleImage = [FLEXUtility circularImageWithColor:[UIColor blackColor] radius:5.0];
  27. self.colorCircleImageView = [[UIImageView alloc] initWithImage:defaultCircleImage];
  28. [self.contentView addSubview:self.colorCircleImageView];
  29. self.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:14.0];
  30. self.detailTextLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  31. self.accessoryType = UITableViewCellAccessoryDetailButton;
  32. self.viewBackgroundColorView = [[UIView alloc] init];
  33. self.viewBackgroundColorView.clipsToBounds = YES;
  34. self.viewBackgroundColorView.layer.borderColor = [UIColor blackColor].CGColor;
  35. self.viewBackgroundColorView.layer.borderWidth = 1.0f;
  36. [self.contentView addSubview:self.viewBackgroundColorView];
  37. }
  38. return self;
  39. }
  40. - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
  41. {
  42. UIColor *originalColour = self.viewBackgroundColorView.backgroundColor;
  43. [super setHighlighted:highlighted animated:animated];
  44. // UITableViewCell changes all subviews in the contentView to backgroundColor = clearColor.
  45. // We want to preserve the hierarchy background color when highlighted.
  46. self.depthIndicatorView.backgroundColor = [FLEXUtility hierarchyIndentPatternColor];
  47. self.viewBackgroundColorView.backgroundColor = originalColour;
  48. }
  49. - (void)setSelected:(BOOL)selected animated:(BOOL)animated
  50. {
  51. UIColor *originalColour = self.viewBackgroundColorView.backgroundColor;
  52. [super setSelected:selected animated:animated];
  53. // See setHighlighted above.
  54. self.depthIndicatorView.backgroundColor = [FLEXUtility hierarchyIndentPatternColor];
  55. self.viewBackgroundColorView.backgroundColor = originalColour;
  56. }
  57. - (void)layoutSubviews
  58. {
  59. [super layoutSubviews];
  60. const CGFloat kContentPadding = 10.0;
  61. const CGFloat kDepthIndicatorWidthMultiplier = 4.0;
  62. const CGFloat kViewBackgroundColourDimension = 20;
  63. CGRect depthIndicatorFrame = CGRectMake(kContentPadding, 0, self.viewDepth * kDepthIndicatorWidthMultiplier, self.contentView.bounds.size.height);
  64. self.depthIndicatorView.frame = depthIndicatorFrame;
  65. CGRect circleFrame = self.colorCircleImageView.frame;
  66. circleFrame.origin.x = CGRectGetMaxX(depthIndicatorFrame);
  67. circleFrame.origin.y = self.textLabel.frame.origin.y + FLEXFloor((self.textLabel.frame.size.height - circleFrame.size.height) / 2.0);
  68. self.colorCircleImageView.frame = circleFrame;
  69. CGRect textLabelFrame = self.textLabel.frame;
  70. CGFloat textOriginX = CGRectGetMaxX(circleFrame) + 4.0;
  71. textLabelFrame.origin.x = textOriginX;
  72. textLabelFrame.size.width = CGRectGetMaxX(self.contentView.frame) - kContentPadding - textOriginX - kViewBackgroundColourDimension;
  73. self.textLabel.frame = textLabelFrame;
  74. CGRect detailTextLabelFrame = self.detailTextLabel.frame;
  75. CGFloat detailOriginX = CGRectGetMaxX(depthIndicatorFrame);
  76. detailTextLabelFrame.origin.x = detailOriginX;
  77. detailTextLabelFrame.size.width = CGRectGetMaxX(self.contentView.bounds) - kContentPadding - detailOriginX;
  78. self.detailTextLabel.frame = detailTextLabelFrame;
  79. CGRect viewBackgroundColourViewFrame = self.textLabel.frame;
  80. viewBackgroundColourViewFrame.size.width = kViewBackgroundColourDimension;
  81. viewBackgroundColourViewFrame.size.height = kViewBackgroundColourDimension;
  82. viewBackgroundColourViewFrame.origin.x = CGRectGetMaxX(self.textLabel.frame) + kContentPadding;
  83. viewBackgroundColourViewFrame.origin.y = ABS(CGRectGetHeight(self.contentView.frame) - CGRectGetHeight(viewBackgroundColourViewFrame)) / 2;
  84. self.viewBackgroundColorView.frame = viewBackgroundColourViewFrame;
  85. self.viewBackgroundColorView.layer.cornerRadius = kViewBackgroundColourDimension / 2;
  86. }
  87. - (void)setViewColor:(UIColor *)viewColor
  88. {
  89. if (![_viewColor isEqual:viewColor]) {
  90. _viewColor = viewColor;
  91. self.colorCircleImageView.image = [FLEXUtility circularImageWithColor:viewColor radius:6.0];
  92. }
  93. }
  94. - (void)setViewDepth:(NSInteger)viewDepth
  95. {
  96. if (_viewDepth != viewDepth) {
  97. _viewDepth = viewDepth;
  98. [self setNeedsLayout];
  99. }
  100. }
  101. @end