FLEXHierarchyTableViewCell.m 4.0 KB

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