You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.7 KiB

  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 "FLEXToolbarItem.h"
  9. #import "FLEXUtility.h"
  10. @interface FLEXToolbarItem ()
  11. @property (nonatomic, copy) NSAttributedString *attributedTitle;
  12. @property (nonatomic, strong) UIImage *image;
  13. @end
  14. @implementation FLEXToolbarItem
  15. - (id)initWithFrame:(CGRect)frame
  16. {
  17. self = [super initWithFrame:frame];
  18. if (self) {
  19. self.backgroundColor = [[self class] defaultBackgroundColor];
  20. [self setTitleColor:[[self class] defaultTitleColor] forState:UIControlStateNormal];
  21. [self setTitleColor:[[self class] disabledTitleColor] forState:UIControlStateDisabled];
  22. }
  23. return self;
  24. }
  25. + (instancetype)toolbarItemWithTitle:(NSString *)title image:(UIImage *)image
  26. {
  27. FLEXToolbarItem *toolbarItem = [self buttonWithType:UIButtonTypeCustom];
  28. NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:[self titleAttributes]];
  29. toolbarItem.attributedTitle = attributedTitle;
  30. toolbarItem.image = image;
  31. [toolbarItem setAttributedTitle:attributedTitle forState:UIControlStateNormal];
  32. [toolbarItem setImage:image forState:UIControlStateNormal];
  33. return toolbarItem;
  34. }
  35. #pragma mark - Display Defaults
  36. + (NSDictionary<NSString *, id> *)titleAttributes
  37. {
  38. return @{NSFontAttributeName : [FLEXUtility defaultFontOfSize:12.0]};
  39. }
  40. + (UIColor *)defaultTitleColor
  41. {
  42. return [UIColor blackColor];
  43. }
  44. + (UIColor *)disabledTitleColor
  45. {
  46. return [UIColor colorWithWhite:121.0/255.0 alpha:1.0];
  47. }
  48. + (UIColor *)highlightedBackgroundColor
  49. {
  50. return [UIColor colorWithWhite:0.9 alpha:1.0];
  51. }
  52. + (UIColor *)selectedBackgroundColor
  53. {
  54. return [UIColor colorWithRed:199.0/255.0 green:199.0/255.0 blue:255.0/255.0 alpha:1.0];
  55. }
  56. + (UIColor *)defaultBackgroundColor
  57. {
  58. return [UIColor clearColor];
  59. }
  60. + (CGFloat)topMargin
  61. {
  62. return 2.0;
  63. }
  64. #pragma mark - State Changes
  65. - (void)setHighlighted:(BOOL)highlighted
  66. {
  67. [super setHighlighted:highlighted];
  68. [self updateBackgroundColor];
  69. }
  70. - (void)setSelected:(BOOL)selected
  71. {
  72. [super setSelected:selected];
  73. [self updateBackgroundColor];
  74. }
  75. - (void)updateBackgroundColor
  76. {
  77. if (self.highlighted) {
  78. self.backgroundColor = [[self class] highlightedBackgroundColor];
  79. } else if (self.selected) {
  80. self.backgroundColor = [[self class] selectedBackgroundColor];
  81. } else {
  82. self.backgroundColor = [[self class] defaultBackgroundColor];
  83. }
  84. }
  85. #pragma mark - UIButton Layout Overrides
  86. - (CGRect)titleRectForContentRect:(CGRect)contentRect
  87. {
  88. // Bottom aligned and centered.
  89. CGRect titleRect = CGRectZero;
  90. CGSize titleSize = [self.attributedTitle boundingRectWithSize:contentRect.size options:0 context:nil].size;
  91. titleSize = CGSizeMake(ceil(titleSize.width), ceil(titleSize.height));
  92. titleRect.size = titleSize;
  93. titleRect.origin.y = contentRect.origin.y + CGRectGetMaxY(contentRect) - titleSize.height;
  94. titleRect.origin.x = contentRect.origin.x + FLEXFloor((contentRect.size.width - titleSize.width) / 2.0);
  95. return titleRect;
  96. }
  97. - (CGRect)imageRectForContentRect:(CGRect)contentRect
  98. {
  99. CGSize imageSize = self.image.size;
  100. CGRect titleRect = [self titleRectForContentRect:contentRect];
  101. CGFloat availableHeight = contentRect.size.height - titleRect.size.height - [[self class] topMargin];
  102. CGFloat originY = [[self class] topMargin] + FLEXFloor((availableHeight - imageSize.height) / 2.0);
  103. CGFloat originX = FLEXFloor((contentRect.size.width - imageSize.width) / 2.0);
  104. CGRect imageRect = CGRectMake(originX, originY, imageSize.width, imageSize.height);
  105. return imageRect;
  106. }
  107. @end