FLEXCarouselCell.m 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // FLEXCarouselCell.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 7/17/19.
  6. // Copyright © 2020 FLEX Team. 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.selectionIndicatorStripe.backgroundColor = self.tintColor;
  24. if (@available(iOS 10, *)) {
  25. self.titleLabel.adjustsFontForContentSizeCategory = YES;
  26. }
  27. [self.contentView addSubview:self.titleLabel];
  28. [self.contentView addSubview:self.selectionIndicatorStripe];
  29. [self installConstraints];
  30. [self updateAppearance];
  31. }
  32. return self;
  33. }
  34. - (void)updateAppearance {
  35. self.selectionIndicatorStripe.hidden = !self.selected;
  36. if (self.selected) {
  37. self.titleLabel.textColor = self.tintColor;
  38. } else {
  39. self.titleLabel.textColor = FLEXColor.deemphasizedTextColor;
  40. }
  41. }
  42. #pragma mark Public
  43. - (NSString *)title {
  44. return self.titleLabel.text;
  45. }
  46. - (void)setTitle:(NSString *)title {
  47. self.titleLabel.text = title;
  48. [self.titleLabel sizeToFit];
  49. [self setNeedsLayout];
  50. }
  51. #pragma mark Overrides
  52. - (void)prepareForReuse {
  53. [super prepareForReuse];
  54. [self updateAppearance];
  55. }
  56. - (void)installConstraints {
  57. CGFloat stripeHeight = 2;
  58. self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
  59. self.selectionIndicatorStripe.translatesAutoresizingMaskIntoConstraints = NO;
  60. UIView *superview = self.contentView;
  61. [self.titleLabel flex_pinEdgesToSuperviewWithInsets:UIEdgeInsetsMake(10, 15, 8 + stripeHeight, 15)];
  62. [self.selectionIndicatorStripe.leadingAnchor constraintEqualToAnchor:superview.leadingAnchor].active = YES;
  63. [self.selectionIndicatorStripe.bottomAnchor constraintEqualToAnchor:superview.bottomAnchor].active = YES;
  64. [self.selectionIndicatorStripe.trailingAnchor constraintEqualToAnchor:superview.trailingAnchor].active = YES;
  65. [self.selectionIndicatorStripe.heightAnchor constraintEqualToConstant:stripeHeight].active = YES;
  66. }
  67. - (void)setSelected:(BOOL)selected {
  68. super.selected = selected;
  69. [self updateAppearance];
  70. }
  71. @end