-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathButton.swift
221 lines (185 loc) · 7.23 KB
/
Button.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//
// Button.swift
// UIKit
//
// Created by Geordie Jay on 16.05.17.
// Copyright © 2017 Geordie Jay. All rights reserved.
//
// Note: we deliberately don't wrap UIButton.
// This allows us to have a somewhat custom API free of objc selectors etc.
fileprivate let labelVerticalPadding: CGFloat = 6
@MainActor
open class Button: UIControl {
internal(set) public var imageView: UIImageView? {
didSet {
oldValue?.removeFromSuperview()
if let imageView = imageView { addSubview(imageView) }
}
}
internal(set) public var titleLabel: UILabel? {
didSet {
oldValue?.removeFromSuperview()
if let titleLabel = titleLabel { addSubview(titleLabel) }
}
}
/// iOS strangely has different behaviour in layoutSubviews depending on whether sizeToFit was (ever) called beforehand
private var sizeToFitWasCalled = false
override open var isSelected: Bool {
didSet { self.setNeedsLayout() }
}
open override var tintColor: UIColor! {
didSet {
if tintColor != nil { setTitleColor(tintColor, for: .normal) }
}
}
override open func sizeToFit() {
updateLabelAndImageForCurrentState()
sizeToFitWasCalled = true
imageView?.sizeToFit()
titleLabel?.sizeToFit()
super.sizeToFit()
}
override open func sizeThatFits(_ size: CGSize) -> CGSize {
guard let titleLabel = titleLabel, let imageView = imageView else {
assertionFailure("titleLabel or imageView should always exist in UIKit-SDL Button")
return size
}
let imageViewIsVisible = !imageView.isHidden
let titleLabelIsVisible = !titleLabel.isHidden
if imageViewIsVisible, titleLabelIsVisible {
return CGSize(
width: imageView.frame.width + titleLabel.frame.width,
height: max(imageView.frame.height, titleLabel.frame.height)
)
} else if imageViewIsVisible, !titleLabelIsVisible {
return CGSize(width: imageView.frame.width, height: imageView.frame.height)
} else if titleLabelIsVisible, !imageViewIsVisible {
return CGSize(
width: titleLabel.frame.width,
height: titleLabel.frame.height + (2 * labelVerticalPadding)
)
} else {
return CGSize(width: 30, height: 34)
}
}
public let tapGestureRecognizer: UITapGestureRecognizer
public var onPress: (@MainActor () -> Void)? {
get { return tapGestureRecognizer.onPress }
set { tapGestureRecognizer.onPress = newValue }
}
@MainActor
public override init(frame: CGRect) {
tapGestureRecognizer = UITapGestureRecognizer(onPress: nil)
super.init(frame: frame)
let titleLabel = UILabel(frame: .zero)
titleLabel.isHidden = true
addSubview(titleLabel)
setTitleColor(.white, for: .normal)
self.titleLabel = titleLabel
let imageView = UIImageView(frame: .zero)
imageView.isHidden = true
imageView.contentMode = .scaleAspectFit
addSubview(imageView)
self.imageView = imageView
tapGestureRecognizer.onTouchesBegan = { [weak self] in
self?.isHighlighted = true
}
tapGestureRecognizer.onTouchesEnded = { [weak self] in
self?.isHighlighted = false
}
tapGestureRecognizer.view = self
addGestureRecognizer(tapGestureRecognizer)
}
fileprivate var images = [UIControlState: UIImage]()
fileprivate var titles = [UIControlState: String]()
fileprivate var titleColors = [UIControlState: UIColor]()
fileprivate var titleShadowColors = [UIControlState: UIColor]()
open override func layoutSubviews() {
updateLabelAndImageForCurrentState()
guard let titleLabel = titleLabel, let imageView = imageView else {
assertionFailure("titleLabel or imageView should always exist in UIKit-SDL Button")
return
}
let titleLabelIsVisible = !titleLabel.isHidden
if titleLabelIsVisible {
if let titleColorForCurrentState = titleColors[state] {
titleLabel.textColor = titleColorForCurrentState
} else if let titleColorForNormalState = titleColors[.normal] {
titleLabel.textColor = titleColorForNormalState
}
if let titleShadowColorForCurrentState = titleShadowColors[state] {
titleLabel.shadowColor = titleShadowColorForCurrentState
} else if let titleShadowColorForNormalState = titleShadowColors[.normal] {
titleLabel.shadowColor = titleShadowColorForNormalState
}
}
titleLabel.setNeedsLayout()
let imageWidth = imageView.frame.width
let labelWidth = titleLabel.frame.width
switch contentHorizontalAlignment {
case .center:
imageView.frame.midX = bounds.midX - (labelWidth / 2)
titleLabel.frame.midX = bounds.midX + (imageWidth / 2)
case .left:
imageView.frame.origin.x = 0
titleLabel.frame.origin.x = imageWidth
case .right:
imageView.frame.maxX = bounds.maxX - labelWidth
titleLabel.frame.maxX = bounds.maxX
}
switch contentVerticalAlignment {
case .center:
imageView.frame.midY = bounds.midY
titleLabel.frame.midY = bounds.midY
case .top:
if imageView.isHidden {
titleLabel.frame.origin.y = sizeToFitWasCalled ? labelVerticalPadding : 0
} else {
titleLabel.frame.origin.y = 0
imageView.frame.origin.y = 0
}
case .bottom:
if imageView.isHidden {
titleLabel.frame.maxY = bounds.maxY - (sizeToFitWasCalled ? labelVerticalPadding : 0)
} else {
titleLabel.frame.maxY = bounds.maxY
imageView.frame.maxY = bounds.maxY
}
}
super.layoutSubviews()
}
}
extension Button {
fileprivate func updateLabelAndImageForCurrentState() {
if let titleForCurrentState = titles[state] {
titleLabel?.text = titleForCurrentState
} else if let titleForNormalState = titles[.normal] {
titleLabel?.text = titleForNormalState
}
if let imageForCurrentState = images[state] {
imageView?.image = imageForCurrentState
} else if let imageForNormalState = images[.normal] {
imageView?.image = imageForNormalState
}
}
}
extension Button {
public func setImage(_ image: UIImage?, for state: UIControlState) {
images[state] = image
imageView?.isHidden = (image == nil)
setNeedsLayout()
}
public func setTitle(_ text: String?, for state: UIControlState) {
titles[state] = text
titleLabel?.isHidden = (text == nil)
setNeedsLayout()
}
public func setTitleColor(_ color: UIColor, for state: UIControlState) {
titleColors[state] = color
setNeedsLayout()
}
public func setTitleShadowColor(_ color: UIColor, for state: UIControlState) {
titleShadowColors[state] = color
setNeedsLayout()
}
}