FLEXExplorerToolbarItem.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. [toolbarItem setTitleColor:FLEXColor.primaryTextColor forState:UIControlStateNormal];
  38. [toolbarItem setTitleColor:FLEXColor.deemphasizedTextColor forState:UIControlStateDisabled];
  39. return toolbarItem;
  40. }
  41. - (FLEXExplorerToolbarItem *)currentItem {
  42. if (!self.enabled && self.sibling) {
  43. return self.sibling.currentItem;
  44. }
  45. return self;
  46. }
  47. #pragma mark - Display Defaults
  48. + (NSDictionary<NSString *, id> *)titleAttributes {
  49. return @{ NSFontAttributeName : [UIFont systemFontOfSize:12.0] };
  50. }
  51. + (UIColor *)highlightedBackgroundColor {
  52. return FLEXColor.toolbarItemHighlightedColor;
  53. }
  54. + (UIColor *)selectedBackgroundColor {
  55. return FLEXColor.toolbarItemSelectedColor;
  56. }
  57. + (UIColor *)defaultBackgroundColor {
  58. return UIColor.clearColor;
  59. }
  60. + (CGFloat)topMargin {
  61. return 2.0;
  62. }
  63. #pragma mark - State Changes
  64. - (void)setHighlighted:(BOOL)highlighted {
  65. super.highlighted = highlighted;
  66. [self updateColors];
  67. }
  68. - (void)setSelected:(BOOL)selected {
  69. super.selected = selected;
  70. [self updateColors];
  71. }
  72. - (void)setEnabled:(BOOL)enabled {
  73. if (self.enabled != enabled) {
  74. if (self.sibling) {
  75. if (enabled) { // Replace sibling with myself
  76. UIView *superview = self.sibling.superview;
  77. [self.sibling removeFromSuperview];
  78. self.frame = self.sibling.frame;
  79. [superview addSubview:self];
  80. } else { // Replace myself with sibling
  81. UIView *superview = self.superview;
  82. [self removeFromSuperview];
  83. self.sibling.frame = self.frame;
  84. [superview addSubview:self.sibling];
  85. }
  86. }
  87. super.enabled = enabled;
  88. }
  89. }
  90. + (id)_selectedIndicatorImage { return nil; }
  91. - (void)updateColors {
  92. // Background color
  93. if (self.highlighted) {
  94. self.backgroundColor = self.class.highlightedBackgroundColor;
  95. } else if (self.selected) {
  96. self.backgroundColor = self.class.selectedBackgroundColor;
  97. } else {
  98. self.backgroundColor = self.class.defaultBackgroundColor;
  99. }
  100. }
  101. #pragma mark - UIButton Layout Overrides
  102. - (CGRect)titleRectForContentRect:(CGRect)contentRect {
  103. NSDictionary *attrs = [[self class] titleAttributes];
  104. // Bottom aligned and centered.
  105. CGRect titleRect = CGRectZero;
  106. CGSize titleSize = [self.title boundingRectWithSize:contentRect.size
  107. options:0
  108. attributes:attrs
  109. context:nil].size;
  110. titleSize = CGSizeMake(ceil(titleSize.width), ceil(titleSize.height));
  111. titleRect.size = titleSize;
  112. titleRect.origin.y = contentRect.origin.y + CGRectGetMaxY(contentRect) - titleSize.height;
  113. titleRect.origin.x = contentRect.origin.x + FLEXFloor((contentRect.size.width - titleSize.width) / 2.0);
  114. return titleRect;
  115. }
  116. - (CGRect)imageRectForContentRect:(CGRect)contentRect {
  117. CGSize imageSize = self.image.size;
  118. CGRect titleRect = [self titleRectForContentRect:contentRect];
  119. CGFloat availableHeight = contentRect.size.height - titleRect.size.height - [[self class] topMargin];
  120. CGFloat originY = [[self class] topMargin] + FLEXFloor((availableHeight - imageSize.height) / 2.0);
  121. CGFloat originX = FLEXFloor((contentRect.size.width - imageSize.width) / 2.0);
  122. CGRect imageRect = CGRectMake(originX, originY, imageSize.width, imageSize.height);
  123. return imageRect;
  124. }
  125. @end