FLEXCarouselCell.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // FLEXCarouselCell.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 7/17/19.
  6. // Copyright © 2019 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXCarouselCell.h"
  9. #import "FLEXColor.h"
  10. #import "UIView+FLEX_Layout.h"
  11. @interface FLEXCarouselCell ()
  12. @property (nonatomic, readonly) UILabel *titleLabel;
  13. @property (nonatomic, readonly) UIView *selectionIndicatorStripe;
  14. @property (nonatomic) BOOL constraintsInstalled;
  15. @end
  16. @implementation FLEXCarouselCell
  17. - (instancetype)initWithFrame:(CGRect)frame {
  18. self = [super initWithFrame:frame];
  19. if (self) {
  20. _titleLabel = [UILabel new];
  21. _selectionIndicatorStripe = [UIView new];
  22. self.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
  23. self.titleLabel.adjustsFontForContentSizeCategory = YES;
  24. self.selectionIndicatorStripe.backgroundColor = self.tintColor;
  25. [self.contentView addSubview:self.titleLabel];
  26. [self.contentView addSubview:self.selectionIndicatorStripe];
  27. [self installConstraints];
  28. [self updateAppearance];
  29. }
  30. return self;
  31. }
  32. - (void)updateAppearance {
  33. self.selectionIndicatorStripe.hidden = !self.selected;
  34. if (self.selected) {
  35. self.titleLabel.textColor = self.tintColor;
  36. } else {
  37. self.titleLabel.textColor = [FLEXColor deemphasizedTextColor];
  38. }
  39. }
  40. #pragma mark Public
  41. - (NSString *)title {
  42. return self.titleLabel.text;
  43. }
  44. - (void)setTitle:(NSString *)title {
  45. self.titleLabel.text = title;
  46. [self.titleLabel sizeToFit];
  47. [self setNeedsLayout];
  48. }
  49. #pragma mark Overrides
  50. - (void)prepareForReuse {
  51. [super prepareForReuse];
  52. [self updateAppearance];
  53. }
  54. - (void)installConstraints {
  55. CGFloat stripeHeight = 2;
  56. self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
  57. self.selectionIndicatorStripe.translatesAutoresizingMaskIntoConstraints = NO;
  58. UIView *superview = self.contentView;
  59. [self.titleLabel pinEdgesToSuperviewWithInsets:UIEdgeInsetsMake(10, 15, 8 + stripeHeight, 15)];
  60. [self.selectionIndicatorStripe.leadingAnchor constraintEqualToAnchor:superview.leadingAnchor].active = YES;
  61. [self.selectionIndicatorStripe.bottomAnchor constraintEqualToAnchor:superview.bottomAnchor].active = YES;
  62. [self.selectionIndicatorStripe.trailingAnchor constraintEqualToAnchor:superview.trailingAnchor].active = YES;
  63. [self.selectionIndicatorStripe.heightAnchor constraintEqualToConstant:stripeHeight].active = YES;
  64. }
  65. - (void)setSelected:(BOOL)selected {
  66. super.selected = selected;
  67. [self updateAppearance];
  68. }
  69. @end