NSMutableArray+SWUtilityButtons.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // NSMutableArray+SWUtilityButtons.m
  3. // SWTableViewCell
  4. //
  5. // Created by Matt Bowman on 11/27/13.
  6. // Copyright (c) 2013 Chris Wendel. All rights reserved.
  7. //
  8. #import "NSMutableArray+SWUtilityButtons.h"
  9. @implementation NSMutableArray (SWUtilityButtons)
  10. - (void)sw_addUtilityButtonWithColor:(UIColor *)color title:(NSString *)title
  11. {
  12. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  13. button.backgroundColor = color;
  14. [button setTitle:title forState:UIControlStateNormal];
  15. [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  16. [button.titleLabel setAdjustsFontSizeToFitWidth:YES];
  17. [self addObject:button];
  18. }
  19. - (void)sw_addUtilityButtonWithColor:(UIColor *)color attributedTitle:(NSAttributedString *)title
  20. {
  21. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  22. button.backgroundColor = color;
  23. [button setAttributedTitle:title forState:UIControlStateNormal];
  24. [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  25. [self addObject:button];
  26. }
  27. - (void)sw_addUtilityButtonWithColor:(UIColor *)color icon:(UIImage *)icon
  28. {
  29. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  30. button.backgroundColor = color;
  31. [button setImage:icon forState:UIControlStateNormal];
  32. [self addObject:button];
  33. }
  34. - (void)sw_addUtilityButtonWithColor:(UIColor *)color normalIcon:(UIImage *)normalIcon selectedIcon:(UIImage *)selectedIcon {
  35. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  36. button.backgroundColor = color;
  37. [button setImage:normalIcon forState:UIControlStateNormal];
  38. [button setImage:selectedIcon forState:UIControlStateHighlighted];
  39. [button setImage:selectedIcon forState:UIControlStateSelected];
  40. [self addObject:button];
  41. }
  42. @end
  43. @implementation NSArray (SWUtilityButtons)
  44. - (BOOL)sw_isEqualToButtons:(NSArray *)buttons
  45. {
  46. buttons = [buttons copy];
  47. if (!buttons || self.count != buttons.count) return NO;
  48. for (NSUInteger idx = 0; idx < self.count; idx++) {
  49. id buttonA = self[idx];
  50. id buttonB = buttons[idx];
  51. if (![buttonA isKindOfClass:[UIButton class]] || ![buttonB isKindOfClass:[UIButton class]]) return NO;
  52. if (![[self class] sw_button:buttonA isEqualToButton:buttonB]) return NO;
  53. }
  54. return YES;
  55. }
  56. + (BOOL)sw_button:(UIButton *)buttonA isEqualToButton:(UIButton *)buttonB
  57. {
  58. if (!buttonA || !buttonB) return NO;
  59. UIColor *backgroundColorA = buttonA.backgroundColor;
  60. UIColor *backgroundColorB = buttonB.backgroundColor;
  61. BOOL haveEqualBackgroundColors = (!backgroundColorA && !backgroundColorB) || [backgroundColorA isEqual:backgroundColorB];
  62. NSString *titleA = [buttonA titleForState:UIControlStateNormal];
  63. NSString *titleB = [buttonB titleForState:UIControlStateNormal];
  64. BOOL haveEqualTitles = (!titleA && !titleB) || [titleA isEqualToString:titleB];
  65. UIImage *normalIconA = [buttonA imageForState:UIControlStateNormal];
  66. UIImage *normalIconB = [buttonB imageForState:UIControlStateNormal];
  67. BOOL haveEqualNormalIcons = (!normalIconA && !normalIconB) || [normalIconA isEqual:normalIconB];
  68. UIImage *selectedIconA = [buttonA imageForState:UIControlStateSelected];
  69. UIImage *selectedIconB = [buttonB imageForState:UIControlStateSelected];
  70. BOOL haveEqualSelectedIcons = (!selectedIconA && !selectedIconB) || [selectedIconA isEqual:selectedIconB];
  71. return haveEqualBackgroundColors && haveEqualTitles && haveEqualNormalIcons && haveEqualSelectedIcons;
  72. }
  73. @end