/
SecondaryRoundedButton.swift
94 lines (74 loc) · 3.18 KB
/
SecondaryRoundedButton.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
import Common
import UIKit
public class SecondaryRoundedButton: ResizableButton, ThemeApplicable {
private struct UX {
static let buttonCornerRadius: CGFloat = 12
static let buttonVerticalInset: CGFloat = 12
static let buttonHorizontalInset: CGFloat = 16
static let buttonFontSize: CGFloat = 16
static let contentInsets = NSDirectionalEdgeInsets(
top: buttonVerticalInset,
leading: buttonHorizontalInset,
bottom: buttonVerticalInset,
trailing: buttonHorizontalInset
)
}
private var highlightedBackgroundColor: UIColor!
private var normalBackgroundColor: UIColor!
private var foregroundColor: UIColor!
override init(frame: CGRect) {
super.init(frame: frame)
configuration = UIButton.Configuration.filled()
titleLabel?.adjustsFontForContentSizeCategory = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override public func updateConfiguration() {
guard var updatedConfiguration = configuration else {
return
}
updatedConfiguration.background.backgroundColor = switch state {
case [.highlighted]:
highlightedBackgroundColor
default:
normalBackgroundColor
}
updatedConfiguration.baseForegroundColor = foregroundColor
updatedConfiguration.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
var container = incoming
container.foregroundColor = updatedConfiguration.baseForegroundColor
container.font = DefaultDynamicFontHelper.preferredBoldFont(
withTextStyle: .callout,
size: UX.buttonFontSize
)
return container
}
configuration = updatedConfiguration
}
public func configure(viewModel: SecondaryRoundedButtonViewModel) {
guard var updatedConfiguration = configuration else {
return
}
updatedConfiguration.contentInsets = UX.contentInsets
updatedConfiguration.title = viewModel.title
updatedConfiguration.titleAlignment = .center
// Using a nil backgroundColorTransformer will just make the background view
// use configuration.background.backgroundColor without any transformation
updatedConfiguration.background.backgroundColorTransformer = nil
updatedConfiguration.background.cornerRadius = UX.buttonCornerRadius
updatedConfiguration.cornerStyle = .fixed
accessibilityIdentifier = viewModel.a11yIdentifier
configuration = updatedConfiguration
}
// MARK: ThemeApplicable
public func applyTheme(theme: Theme) {
highlightedBackgroundColor = theme.colors.actionSecondaryHover
normalBackgroundColor = theme.colors.actionSecondary
foregroundColor = theme.colors.textOnLight
setNeedsUpdateConfiguration()
}
}