-
Notifications
You must be signed in to change notification settings - Fork 14
/
TvOSMoreButton.swift
321 lines (270 loc) · 9.99 KB
/
TvOSMoreButton.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//
// TvOSMoreButton.swift
//
// Copyright (c) 2016-2019 Chris Goldsby
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import Foundation
import UIKit
@objc
open class TvOSMoreButton: UIView {
/*
* FocusableMode.Auto
* Focus is allowed only when the text does not fit on the label.
*/
public enum FocusableMode: Equatable {
case auto
case manual(_ isFocusable: Bool)
}
private lazy var contentView: UIView = {
let contentView = UIView(frame: bounds)
return contentView
}()
private var label: UILabel!
private var focusedView: UIView!
private var selectGestureRecognizer: UIGestureRecognizer!
private var isFocusable = false {
didSet {
if isFocusable {
accessibilityTraits.remove(.notEnabled)
}
else {
accessibilityTraits.insert(.notEnabled)
}
}
}
open var focusableMode: FocusableMode = .auto {
didSet {
switch focusableMode {
case .manual(let isFocusable):
self.isFocusable = isFocusable
case .auto:
updateUI()
}
}
}
@objc open var text: String? {
didSet { updateUI() }
}
@objc open var textColor = UIColor.white {
didSet {
label.textColor = textColor
updateUI()
}
}
@objc open var font = UIFont.systemFont(ofSize: 25) {
didSet {
label.font = font
updateUI()
}
}
@objc open var textAlignment = NSTextAlignment.natural {
didSet { label.textAlignment = textAlignment }
}
@objc open var ellipsesString = String.TvOSMoreButton.ellipses.🌍 {
didSet { updateUI() }
}
@objc open var trailingText = String.TvOSMoreButton.more.🌍 {
didSet { updateUI() }
}
@objc open var trailingTextColor = UIColor.black.withAlphaComponent(0.5) {
didSet { updateUI() }
}
@objc open var trailingTextFont = UIFont.boldSystemFont(ofSize: 18) {
didSet { updateUI() }
}
@objc open var contentInset = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12) {
didSet {
guard contentView.superview != nil else { return }
contentView.removeFromSuperview()
addSubview(contentView)
contentView.pinToSuperview(insets: contentInset)
updateUI()
}
}
@available(*, deprecated, renamed: "contentInset")
@objc open var labelMargin = CGFloat(12.0) {
didSet {
contentInset = UIEdgeInsets(
top: labelMargin,
left: labelMargin,
bottom: labelMargin,
right: labelMargin
)
}
}
@objc open var pressAnimationDuration = 0.1
@objc open var cornerRadius = CGFloat(10.0)
@objc open var focusedScaleFactor = CGFloat(1.05)
@objc open var shadowRadius = CGFloat(10)
@objc open var shadowColor = UIColor.black.cgColor
@objc open var focusedShadowOffset = CGSize(width: 0, height: 27)
@objc open var focusedShadowOpacity = Float(0.75)
@objc open var focusedViewAlpha = CGFloat(0.75)
@objc open var buttonWasPressed: ((String?) -> Void)?
private var textAttributes: [NSAttributedString.Key: Any] {
return [
.foregroundColor: textColor,
.font: font
]
}
private var trailingTextAttributes: [NSAttributedString.Key: Any] {
return [
.foregroundColor: trailingTextColor,
.font: trailingTextFont
]
}
public override init(frame: CGRect) {
super.init(frame: frame)
setUpUI()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setUpUI()
}
override open var intrinsicContentSize: CGSize {
return label.intrinsicContentSize
}
override open var canBecomeFocused: Bool {
return isFocusable
}
override open func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
coordinator.addCoordinatedAnimations({
self.isFocused ? self.applyFocusedAppearance() : self.applyUnfocusedAppearance()
})
}
open func updateUI() {
truncateAndUpdateText()
}
// MARK: - Presses
override open func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
super.pressesBegan(presses, with: event)
for item in presses where item.type == .select {
applyPressDownAppearance()
}
}
override open func pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
super.pressesEnded(presses, with: event)
for item in presses where item.type == .select {
applyPressUpAppearance()
}
}
override open func pressesCancelled(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
for item in presses where item.type == .select {
applyPressUpAppearance()
}
}
// MARK: - Private
private func setUpUI() {
setUpView()
setUpFocusedView()
setUpContentView()
setUpLabel()
setUpSelectGestureRecognizer()
applyUnfocusedAppearance()
}
private func setUpView() {
isUserInteractionEnabled = true
backgroundColor = .clear
clipsToBounds = false
isAccessibilityElement = true
accessibilityTraits = UIAccessibilityTraits.button
accessibilityIdentifier = "tvos more button"
}
private func setUpContentView() {
addSubview(contentView)
contentView.pinToSuperview(insets: contentInset)
}
private func setUpLabel() {
label = UILabel(frame: bounds)
label.numberOfLines = 0
contentView.addSubview(label)
label.pinToSuperview()
}
private func setUpFocusedView() {
focusedView = UIView(frame: bounds)
focusedView.layer.cornerRadius = cornerRadius
focusedView.layer.shadowColor = shadowColor
focusedView.layer.shadowRadius = shadowRadius
addSubview(focusedView)
focusedView.pinToSuperview()
let blurEffect = UIBlurEffect(style: .light)
let blurredView = UIVisualEffectView(effect: blurEffect)
blurredView.frame = bounds
blurredView.alpha = focusedViewAlpha
blurredView.layer.cornerRadius = cornerRadius
blurredView.layer.masksToBounds = true
focusedView.addSubview(blurredView)
blurredView.pinToSuperview()
}
private func setUpSelectGestureRecognizer() {
selectGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(selectGestureWasPressed))
selectGestureRecognizer.allowedPressTypes = [NSNumber(value: UIPress.PressType.select.rawValue)]
addGestureRecognizer(selectGestureRecognizer)
}
@objc private func selectGestureWasPressed() {
buttonWasPressed?(text)
}
// MARK: - Focus Appearance
func applyFocusedAppearance() {
transform = CGAffineTransform(scaleX: focusedScaleFactor, y: focusedScaleFactor)
focusedView.layer.shadowOffset = focusedShadowOffset
focusedView.layer.shadowOpacity = focusedShadowOpacity
focusedView.alpha = 1
}
func applyUnfocusedAppearance() {
transform = CGAffineTransform.identity
focusedView.layer.shadowOffset = .zero
focusedView.layer.shadowOpacity = 0
focusedView.alpha = 0
}
private func applyPressUpAppearance() {
UIView.animate(withDuration: pressAnimationDuration) {
self.isFocused ? self.applyFocusedAppearance() : self.applyUnfocusedAppearance()
}
}
private func applyPressDownAppearance() {
UIView.animate(withDuration: pressAnimationDuration) {
self.transform = CGAffineTransform.identity
self.focusedView.layer.shadowOffset = .zero
self.focusedView.layer.shadowOpacity = 0
}
}
// MARK: - Truncating Text
private func truncateAndUpdateText() {
label.text = text
accessibilityLabel = text
guard let text = text, !text.isEmpty else { return }
layoutIfNeeded()
let labelSize = label.bounds.size
let trailingText = " " + self.trailingText
label.attributedText = text.truncateToSize(size: labelSize,
ellipsesString: ellipsesString,
trailingText: trailingText,
attributes: textAttributes,
trailingTextAttributes: trailingTextAttributes)
accessibilityLabel = label.attributedText?.string
if case .auto = focusableMode {
isFocusable = !text.willFit(to: labelSize,
attributes: textAttributes,
trailingTextAttributes: trailingTextAttributes)
}
}
}