FLEXToolbarItem.m 4.9 KB

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