FLEXToolbarItem.m 3.5 KB

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