FLEXHierarchyTableViewCell.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. }
  33. return self;
  34. }
  35. - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
  36. {
  37. [super setHighlighted:highlighted animated:animated];
  38. // UITableViewCell changes all subviews in the contentView to backgroundColor = clearColor.
  39. // We want to preserve the hierarchy background color when highlighted.
  40. self.depthIndicatorView.backgroundColor = [FLEXUtility hierarchyIndentPatternColor];
  41. }
  42. - (void)setSelected:(BOOL)selected animated:(BOOL)animated
  43. {
  44. [super setSelected:selected animated:animated];
  45. // See setHighlighted above.
  46. self.depthIndicatorView.backgroundColor = [FLEXUtility hierarchyIndentPatternColor];
  47. }
  48. - (void)layoutSubviews
  49. {
  50. [super layoutSubviews];
  51. const CGFloat kContentPadding = 10.0;
  52. const CGFloat kDepthIndicatorWidthMultiplier = 4.0;
  53. CGRect depthIndicatorFrame = CGRectMake(kContentPadding, 0, self.viewDepth * kDepthIndicatorWidthMultiplier, self.contentView.bounds.size.height);
  54. self.depthIndicatorView.frame = depthIndicatorFrame;
  55. CGRect circleFrame = self.colorCircleImageView.frame;
  56. circleFrame.origin.x = CGRectGetMaxX(depthIndicatorFrame);
  57. circleFrame.origin.y = self.textLabel.frame.origin.y + FLEXFloor((self.textLabel.frame.size.height - circleFrame.size.height) / 2.0);
  58. self.colorCircleImageView.frame = circleFrame;
  59. CGRect textLabelFrame = self.textLabel.frame;
  60. CGFloat textOriginX = CGRectGetMaxX(circleFrame) + 4.0;
  61. textLabelFrame.origin.x = textOriginX;
  62. textLabelFrame.size.width = CGRectGetMaxX(self.contentView.bounds) - kContentPadding - textOriginX;
  63. self.textLabel.frame = textLabelFrame;
  64. CGRect detailTextLabelFrame = self.detailTextLabel.frame;
  65. CGFloat detailOriginX = CGRectGetMaxX(depthIndicatorFrame);
  66. detailTextLabelFrame.origin.x = detailOriginX;
  67. detailTextLabelFrame.size.width = CGRectGetMaxX(self.contentView.bounds) - kContentPadding - detailOriginX;
  68. self.detailTextLabel.frame = detailTextLabelFrame;
  69. }
  70. - (void)setViewColor:(UIColor *)viewColor
  71. {
  72. if (![_viewColor isEqual:viewColor]) {
  73. _viewColor = viewColor;
  74. self.colorCircleImageView.image = [FLEXUtility circularImageWithColor:viewColor radius:6.0];
  75. }
  76. }
  77. - (void)setViewDepth:(NSInteger)viewDepth
  78. {
  79. if (_viewDepth != viewDepth) {
  80. _viewDepth = viewDepth;
  81. [self setNeedsLayout];
  82. }
  83. }
  84. @end