-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIButton+Extensions.swift
197 lines (165 loc) · 6.89 KB
/
UIButton+Extensions.swift
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
// UIButton+Extensions.swift
// OYExtensions
//
// Created by osmanyildirim
//
import UIKit
extension UIButton: OYInit {
public typealias ViewType = UIButton
/// Get and Set title of `normal` state for UIButton
public var oy_titleForNormal: String? {
get { title(for: .normal) }
set(value) { setTitle(value, for: .normal) }
}
/// Get and Set title of `disabled` state for UIButton
public var oy_titleForDisabled: String? {
get { title(for: .disabled) }
set(value) { setTitle(value, for: .disabled) }
}
/// Get and Set title of `highlighted` state for UIButton
public var oy_titleForHighlighted: String? {
get { title(for: .highlighted) }
set(value) { setTitle(value, for: .highlighted) }
}
/// Get and Set title of `selected` state for UIButton
public var oy_titleForSelected: String? {
get { title(for: .selected) }
set(value) { setTitle(value, for: .selected) }
}
/// Get and Set title color of `normal` state for UIButton
public var oy_titleColorForNormal: UIColor? {
get { titleColor(for: .normal) }
set(value) { setTitleColor(value, for: .normal) }
}
/// Get and Set title color of `disabled` state for UIButton
public var oy_titleColorForDisabled: UIColor? {
get { titleColor(for: .disabled) }
set(value) { setTitleColor(value, for: .disabled) }
}
/// Get and Set title color of `highlighted` state for UIButton
public var oy_titleColorForHighlighted: UIColor? {
get { titleColor(for: .highlighted) }
set(value) { setTitleColor(value, for: .highlighted) }
}
/// Get and Set title color of `selected` state for UIButton
public var oy_titleColorForSelected: UIColor? {
get { titleColor(for: .selected) }
set(value) { setTitleColor(value, for: .selected) }
}
/// Get and Set title font for UIButton
public var oy_titleFont: UIFont? {
get { titleLabel?.font }
set(value) { titleLabel?.font = value }
}
/// Get and Set background color of `normal` state for UIButton
public var oy_backgroundColorForNormal: UIColor? {
get { backgroundColor }
set(value) { oy_setBackgroundColor(value, for: .normal) }
}
/// Set background color of `disabled` state for UIButton
public var oy_backgroundColorForDisabled: UIColor? {
get { backgroundColor }
set(value) { oy_setBackgroundColor(value, for: .disabled) }
}
/// Set background color of `highlighted` state for UIButton
public var oy_backgroundColorForHighlighted: UIColor? {
get { backgroundColor }
set(value) { oy_setBackgroundColor(value, for: .highlighted) }
}
/// Set background color of `selected` state for UIButton
public var oy_backgroundColorForSelected: UIColor? {
get { backgroundColor }
set(value) { oy_setBackgroundColor(value, for: .selected) }
}
/// Get and Set image of `normal` state for UIButton
public var oy_imageForNormal: UIImage? {
get { image(for: .normal) }
set(value) { setImage(value, for: .normal) }
}
/// Get and Set image of `disabled` state for UIButton
public var oy_imageForDisabled: UIImage? {
get { image(for: .disabled) }
set(value) { setImage(value, for: .disabled) }
}
/// Get and Set image of `highlighted` state for UIButton
public var oy_imageForHighlighted: UIImage? {
get { image(for: .highlighted) }
set(value) { setImage(value, for: .highlighted) }
}
/// Get and Set image of `selected` state for UIButton
public var oy_imageForSelected: UIImage? {
get { image(for: .selected) }
set(value) { setImage(value, for: .selected) }
}
/// Get and Set background image of `normal` state for UIButton
public var oy_backgroundImageForNormal: UIImage? {
get { backgroundImage(for: .normal) }
set(value) { setBackgroundImage(value, for: .normal) }
}
/// Set background image of `disabled` state for UIButton
public var oy_backgroundImageForDisabled: UIImage? {
get { backgroundImage(for: .disabled) }
set(value) { setBackgroundImage(value, for: .disabled) }
}
/// Set background image of `highlighted` state for UIButton
public var oy_backgroundImageForHighlighted: UIImage? {
get { backgroundImage(for: .highlighted) }
set(value) { setBackgroundImage(value, for: .highlighted) }
}
/// Set background image of `selected` state for UIButton
public var oy_backgroundImageForSelected: UIImage? {
get { backgroundImage(for: .selected) }
set(value) { setBackgroundImage(value, for: .selected) }
}
/// Get and Set attributed title color of `normal` state for UIButton
public var oy_attributedTitleForNormal: NSAttributedString? {
get { attributedTitle(for: .normal) }
set { setAttributedTitle(newValue, for: .normal) }
}
/// Get and Set attributed title color of `disabled` state for UIButton
public var oy_attributedTitleForDisabled: NSAttributedString? {
get { attributedTitle(for: .disabled) }
set { setAttributedTitle(newValue, for: .disabled) }
}
/// Get and Set attributed title color of `highlighted` state for UIButton
public var oy_attributedTitleForHighlighted: NSAttributedString? {
get { attributedTitle(for: .highlighted) }
set { setAttributedTitle(newValue, for: .highlighted) }
}
/// Get and Set attributed title color of `selected` state for UIButton
public var oy_attributedTitleForSelected: NSAttributedString? {
get { attributedTitle(for: .selected) }
set { setAttributedTitle(newValue, for: .selected) }
}
/// `button.oy_touchUpInside {
/// // do stuff
/// `}
public func oy_touchUpInside(closure: @escaping() -> Void) {
oy_event(for: .touchUpInside) {
closure()
}
}
/// `btn3.oy_addAction(action: .touchUpOutside) {
/// // do stuff
/// `}
public func oy_addAction(action: UIControl.Event, closure: @escaping() -> Void) {
oy_event(for: action) {
closure()
}
}
/// remove the target/action for a set of events
public func oy_removeTargetsAndActions() {
removeTarget(nil, action: nil, for: .allEvents)
}
/// Private method of UIButton background color setters
private func oy_setBackgroundColor(_ color: UIColor?, for state: UIControl.State = .normal) {
UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
guard let context = UIGraphicsGetCurrentContext(), let color = color else { return }
context.setFillColor(color.cgColor)
context.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
let colorImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
setBackgroundImage(colorImage, for: state)
}
}