FLEXToolbarItem.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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, copy) NSString *title;
  13. @property (nonatomic) UIImage *image;
  14. @end
  15. @implementation FLEXToolbarItem
  16. + (instancetype)toolbarItemWithTitle:(NSString *)title image:(UIImage *)image
  17. {
  18. FLEXToolbarItem *toolbarItem = [self buttonWithType:UIButtonTypeSystem];
  19. toolbarItem.title = title;
  20. toolbarItem.backgroundColor = [self defaultBackgroundColor];
  21. toolbarItem.image = image;
  22. toolbarItem.titleLabel.font = [FLEXUtility defaultFontOfSize:12.0];
  23. [toolbarItem setTitle:title forState:UIControlStateNormal];
  24. [toolbarItem setImage:image forState:UIControlStateNormal];
  25. [toolbarItem setTitleColor:[FLEXColor primaryTextColor] forState:UIControlStateNormal];
  26. [toolbarItem setTitleColor:[FLEXColor deemphasizedTextColor] forState:UIControlStateDisabled];
  27. [toolbarItem setTintColor:[FLEXColor iconColor]];
  28. return toolbarItem;
  29. }
  30. #pragma mark - Display Defaults
  31. + (NSDictionary<NSString *, id> *)titleAttributes
  32. {
  33. return @{NSFontAttributeName : [FLEXUtility defaultFontOfSize:12.0]};
  34. }
  35. + (UIColor *)highlightedBackgroundColor
  36. {
  37. return [FLEXColor toolbarItemHighlightedColor];
  38. }
  39. + (UIColor *)selectedBackgroundColor
  40. {
  41. return [FLEXColor toolbarItemSelectedColor];
  42. }
  43. + (UIColor *)defaultBackgroundColor
  44. {
  45. return UIColor.clearColor;
  46. }
  47. + (CGFloat)topMargin
  48. {
  49. return 2.0;
  50. }
  51. #pragma mark - State Changes
  52. - (void)setHighlighted:(BOOL)highlighted
  53. {
  54. [super setHighlighted:highlighted];
  55. [self updateColors];
  56. }
  57. - (void)setSelected:(BOOL)selected
  58. {
  59. [super setSelected:selected];
  60. [self updateColors];
  61. }
  62. + (id)_selectedIndicatorImage { return nil; }
  63. - (void)updateColors
  64. {
  65. // Background color
  66. if (self.highlighted) {
  67. self.backgroundColor = [[self class] highlightedBackgroundColor];
  68. } else if (self.selected) {
  69. self.backgroundColor = [[self class] selectedBackgroundColor];
  70. } else {
  71. self.backgroundColor = [[self class] defaultBackgroundColor];
  72. }
  73. }
  74. #pragma mark - UIButton Layout Overrides
  75. - (CGRect)titleRectForContentRect:(CGRect)contentRect
  76. {
  77. NSDictionary *attrs = [[self class] titleAttributes];
  78. // Bottom aligned and centered.
  79. CGRect titleRect = CGRectZero;
  80. CGSize titleSize = [self.title boundingRectWithSize:contentRect.size
  81. options:0
  82. attributes:attrs
  83. context:nil].size;
  84. titleSize = CGSizeMake(ceil(titleSize.width), ceil(titleSize.height));
  85. titleRect.size = titleSize;
  86. titleRect.origin.y = contentRect.origin.y + CGRectGetMaxY(contentRect) - titleSize.height;
  87. titleRect.origin.x = contentRect.origin.x + FLEXFloor((contentRect.size.width - titleSize.width) / 2.0);
  88. return titleRect;
  89. }
  90. - (CGRect)imageRectForContentRect:(CGRect)contentRect
  91. {
  92. CGSize imageSize = self.image.size;
  93. CGRect titleRect = [self titleRectForContentRect:contentRect];
  94. CGFloat availableHeight = contentRect.size.height - titleRect.size.height - [[self class] topMargin];
  95. CGFloat originY = [[self class] topMargin] + FLEXFloor((availableHeight - imageSize.height) / 2.0);
  96. CGFloat originX = FLEXFloor((contentRect.size.width - imageSize.width) / 2.0);
  97. CGRect imageRect = CGRectMake(originX, originY, imageSize.width, imageSize.height);
  98. return imageRect;
  99. }
  100. @end