FLEXToolbarItem.m 3.5 KB

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