The iOS framework that grows only as fast as its documentation
NIButtonUtilities.m
1 //
2 // Copyright 2011-2014 NimbusKit
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #import "NIButtonUtilities.h"
18 
19 void NIApplyImageSelectorToButton(SEL selector, id target, UIButton* button) {
20  IMP method = [target methodForSelector:selector];
21  UIImage* image = nil;
22 
23  image = method(target, selector, UIControlStateNormal);
24  [button setImage:image forState:UIControlStateNormal];
25 
26  image = method(target, selector, UIControlStateHighlighted);
27  [button setImage:image forState:UIControlStateHighlighted];
28 
29  image = method(target, selector, UIControlStateDisabled);
30  [button setImage:image forState:UIControlStateDisabled];
31 
32  image = method(target, selector, UIControlStateSelected);
33  [button setImage:image forState:UIControlStateSelected];
34 
35  UIControlState selectedHighlightState = UIControlStateSelected | UIControlStateHighlighted;
36  image = method(target, selector, selectedHighlightState);
37  [button setImage:image forState:selectedHighlightState];
38 }
39 
40 void NIApplyBackgroundImageSelectorToButton(SEL selector, id target, UIButton* button) {
41  IMP method = [target methodForSelector:selector];
42  UIImage* image = nil;
43 
44  image = method(target, selector, UIControlStateNormal);
45  [button setBackgroundImage:image forState:UIControlStateNormal];
46 
47  image = method(target, selector, UIControlStateHighlighted);
48  [button setBackgroundImage:image forState:UIControlStateHighlighted];
49 
50  image = method(target, selector, UIControlStateDisabled);
51  [button setBackgroundImage:image forState:UIControlStateDisabled];
52 
53  image = method(target, selector, UIControlStateSelected);
54  [button setBackgroundImage:image forState:UIControlStateSelected];
55 
56  UIControlState selectedHighlightState = UIControlStateSelected | UIControlStateHighlighted;
57  image = method(target, selector, selectedHighlightState);
58  [button setBackgroundImage:image forState:selectedHighlightState];
59 }
60 
61 void NIApplyTitleColorSelectorToButton(SEL selector, id target, UIButton* button) {
62  IMP method = [target methodForSelector:selector];
63  UIColor* color = nil;
64 
65  color = method(target, selector, UIControlStateNormal);
66  [button setTitleColor:color forState:UIControlStateNormal];
67 
68  color = method(target, selector, UIControlStateHighlighted);
69  [button setTitleColor:color forState:UIControlStateHighlighted];
70 
71  color = method(target, selector, UIControlStateDisabled);
72  [button setTitleColor:color forState:UIControlStateDisabled];
73 
74  color = method(target, selector, UIControlStateSelected);
75  [button setTitleColor:color forState:UIControlStateSelected];
76 
77  UIControlState selectedHighlightState = UIControlStateSelected | UIControlStateHighlighted;
78  color = method(target, selector, selectedHighlightState);
79  [button setTitleColor:color forState:selectedHighlightState];
80 }
void NIApplyTitleColorSelectorToButton(SEL selector, id target, UIButton *button)
Sets the title colors for a button's states.
void NIApplyImageSelectorToButton(SEL selector, id target, UIButton *button)
Sets the images for a button's states.
void NIApplyBackgroundImageSelectorToButton(SEL selector, id target, UIButton *button)
Sets the background images for a button's states.