FLEXExplorerToolbarItem.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // FLEXExplorerToolbarItem.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 4/4/14.
  6. // Copyright (c) 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXColor.h"
  9. #import "FLEXExplorerToolbarItem.h"
  10. #import "FLEXUtility.h"
  11. @interface FLEXExplorerToolbarItem ()
  12. @property (nonatomic) FLEXExplorerToolbarItem *sibling;
  13. @property (nonatomic, copy) NSString *title;
  14. @property (nonatomic) UIImage *image;
  15. @property (nonatomic, readonly, class) UIColor *defaultBackgroundColor;
  16. @property (nonatomic, readonly, class) UIColor *highlightedBackgroundColor;
  17. @property (nonatomic, readonly, class) UIColor *selectedBackgroundColor;
  18. @end
  19. @implementation FLEXExplorerToolbarItem
  20. #pragma mark - Public
  21. + (instancetype)itemWithTitle:(NSString *)title image:(UIImage *)image {
  22. return [self itemWithTitle:title image:image sibling:nil];
  23. }
  24. + (instancetype)itemWithTitle:(NSString *)title image:(UIImage *)image sibling:(FLEXExplorerToolbarItem *)backupItem {
  25. #if !TARGET_OS_TV
  26. NSParameterAssert(title); NSParameterAssert(image);
  27. #endif
  28. FLEXExplorerToolbarItem *toolbarItem = [self buttonWithType:UIButtonTypeSystem];
  29. toolbarItem.sibling = backupItem;
  30. toolbarItem.title = title;
  31. toolbarItem.image = image;
  32. toolbarItem.tintColor = FLEXColor.iconColor;
  33. toolbarItem.backgroundColor = self.defaultBackgroundColor;
  34. toolbarItem.titleLabel.font = [UIFont systemFontOfSize:12.0];
  35. [toolbarItem setTitle:title forState:UIControlStateNormal];
  36. [toolbarItem setImage:image forState:UIControlStateNormal];
  37. #if !TARGET_OS_TV
  38. [toolbarItem setTitleColor:FLEXColor.primaryTextColor forState:UIControlStateNormal];
  39. #endif
  40. [toolbarItem setTitleColor:FLEXColor.deemphasizedTextColor forState:UIControlStateDisabled];
  41. return toolbarItem;
  42. }
  43. - (FLEXExplorerToolbarItem *)currentItem {
  44. if (!self.enabled && self.sibling) {
  45. return self.sibling.currentItem;
  46. }
  47. return self;
  48. }
  49. #pragma mark - Display Defaults
  50. + (NSDictionary<NSString *, id> *)titleAttributes {
  51. return @{ NSFontAttributeName : [UIFont systemFontOfSize:12.0] };
  52. }
  53. + (UIColor *)highlightedBackgroundColor {
  54. return FLEXColor.toolbarItemHighlightedColor;
  55. }
  56. + (UIColor *)selectedBackgroundColor {
  57. return FLEXColor.toolbarItemSelectedColor;
  58. }
  59. + (UIColor *)defaultBackgroundColor {
  60. return UIColor.clearColor;
  61. }
  62. + (CGFloat)topMargin {
  63. return 20.0;
  64. }
  65. #pragma mark - State Changes
  66. - (void)setHighlighted:(BOOL)highlighted {
  67. super.highlighted = highlighted;
  68. [self updateColors];
  69. }
  70. - (void)setSelected:(BOOL)selected {
  71. super.selected = selected;
  72. [self updateColors];
  73. }
  74. - (void)setEnabled:(BOOL)enabled {
  75. if (self.enabled != enabled) {
  76. if (self.sibling) {
  77. if (enabled) { // Replace sibling with myself
  78. UIView *superview = self.sibling.superview;
  79. [self.sibling removeFromSuperview];
  80. self.frame = self.sibling.frame;
  81. [superview addSubview:self];
  82. } else { // Replace myself with sibling
  83. UIView *superview = self.superview;
  84. [self removeFromSuperview];
  85. self.sibling.frame = self.frame;
  86. [superview addSubview:self.sibling];
  87. }
  88. }
  89. super.enabled = enabled;
  90. }
  91. }
  92. + (id)_selectedIndicatorImage { return nil; }
  93. - (void)updateColors {
  94. // Background color
  95. if (self.highlighted) {
  96. self.backgroundColor = self.class.highlightedBackgroundColor;
  97. } else if (self.selected) {
  98. #if !TARGET_OS_TV
  99. self.backgroundColor = self.class.selectedBackgroundColor;
  100. #endif
  101. } else {
  102. self.backgroundColor = self.class.defaultBackgroundColor;
  103. }
  104. }
  105. #pragma mark - UIButton Layout Overrides
  106. - (CGRect)titleRectForContentRect:(CGRect)contentRect {
  107. CGFloat padding = 0;
  108. CGFloat titleBottomPadding = 0;
  109. #if TARGET_OS_TV
  110. padding = 30;
  111. titleBottomPadding = 5;
  112. #endif
  113. NSDictionary *attrs = [[self class] titleAttributes];
  114. // Bottom aligned and centered.
  115. CGRect titleRect = CGRectZero;
  116. CGSize titleSize = [self.title boundingRectWithSize:contentRect.size
  117. options:0
  118. attributes:attrs
  119. context:nil].size;
  120. titleSize = CGSizeMake(ceil(titleSize.width), ceil(titleSize.height));
  121. titleRect.size = titleSize;
  122. titleRect.origin.y = contentRect.origin.y + CGRectGetMaxY(contentRect) - titleSize.height - titleBottomPadding;
  123. titleRect.origin.x = contentRect.origin.x + FLEXFloor((contentRect.size.width-padding - titleSize.width) / 2.0);
  124. return titleRect;
  125. }
  126. #if !TARGET_OS_TV
  127. - (CGRect)imageRectForContentRect:(CGRect)contentRect {
  128. CGSize imageSize = self.image.size;
  129. CGRect titleRect = [self titleRectForContentRect:contentRect];
  130. CGFloat availableHeight = contentRect.size.height - titleRect.size.height - [[self class] topMargin];
  131. CGFloat originY = [[self class] topMargin] + FLEXFloor((availableHeight - imageSize.height) / 2.0);
  132. CGFloat originX = FLEXFloor((contentRect.size.width - imageSize.width) / 2.0);
  133. CGRect imageRect = CGRectMake(originX, originY, imageSize.width, imageSize.height);
  134. return imageRect;
  135. }
  136. #endif
  137. @end