-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathNSMutableArray+SWUtilityButtons.m
executable file
·102 lines (82 loc) · 3.91 KB
/
NSMutableArray+SWUtilityButtons.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
// NSMutableArray+SWUtilityButtons.m
// SWTableViewCell
//
// Created by Matt Bowman on 11/27/13.
// Copyright (c) 2013 Chris Wendel. All rights reserved.
//
#import "NSMutableArray+SWUtilityButtons.h"
@implementation NSMutableArray (SWUtilityButtons)
- (void)sw_addUtilityButtonWithColor:(UIColor *)color title:(NSString *)title
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = color;
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button.titleLabel setAdjustsFontSizeToFitWidth:YES];
[self addObject:button];
}
- (void)sw_addUtilityButtonWithColor:(UIColor *)color title:(NSString *)title titleColor:(UIColor *)titleColor
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = color;
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:titleColor forState:UIControlStateNormal];
[button.titleLabel setAdjustsFontSizeToFitWidth:YES];
[self addObject:button];
}
- (void)sw_addUtilityButtonWithColor:(UIColor *)color attributedTitle:(NSAttributedString *)title
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = color;
[button setAttributedTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self addObject:button];
}
- (void)sw_addUtilityButtonWithColor:(UIColor *)color icon:(UIImage *)icon
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = color;
[button setImage:icon forState:UIControlStateNormal];
[self addObject:button];
}
- (void)sw_addUtilityButtonWithColor:(UIColor *)color normalIcon:(UIImage *)normalIcon selectedIcon:(UIImage *)selectedIcon {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = color;
[button setImage:normalIcon forState:UIControlStateNormal];
[button setImage:selectedIcon forState:UIControlStateHighlighted];
[button setImage:selectedIcon forState:UIControlStateSelected];
[self addObject:button];
}
@end
@implementation NSArray (SWUtilityButtons)
- (BOOL)sw_isEqualToButtons:(NSArray *)buttons
{
buttons = [buttons copy];
if (!buttons || self.count != buttons.count) return NO;
for (NSUInteger idx = 0; idx < self.count; idx++) {
id buttonA = self[idx];
id buttonB = buttons[idx];
if (![buttonA isKindOfClass:[UIButton class]] || ![buttonB isKindOfClass:[UIButton class]]) return NO;
if (![[self class] sw_button:buttonA isEqualToButton:buttonB]) return NO;
}
return YES;
}
+ (BOOL)sw_button:(UIButton *)buttonA isEqualToButton:(UIButton *)buttonB
{
if (!buttonA || !buttonB) return NO;
UIColor *backgroundColorA = buttonA.backgroundColor;
UIColor *backgroundColorB = buttonB.backgroundColor;
BOOL haveEqualBackgroundColors = (!backgroundColorA && !backgroundColorB) || [backgroundColorA isEqual:backgroundColorB];
NSString *titleA = [buttonA titleForState:UIControlStateNormal];
NSString *titleB = [buttonB titleForState:UIControlStateNormal];
BOOL haveEqualTitles = (!titleA && !titleB) || [titleA isEqualToString:titleB];
UIImage *normalIconA = [buttonA imageForState:UIControlStateNormal];
UIImage *normalIconB = [buttonB imageForState:UIControlStateNormal];
BOOL haveEqualNormalIcons = (!normalIconA && !normalIconB) || [normalIconA isEqual:normalIconB];
UIImage *selectedIconA = [buttonA imageForState:UIControlStateSelected];
UIImage *selectedIconB = [buttonB imageForState:UIControlStateSelected];
BOOL haveEqualSelectedIcons = (!selectedIconA && !selectedIconB) || [selectedIconA isEqual:selectedIconB];
return haveEqualBackgroundColors && haveEqualTitles && haveEqualNormalIcons && haveEqualSelectedIcons;
}
@end