@@ -11,12 +11,12 @@ import UIKit
11
11
/// A subclass that implements action buttons used across the app
12
12
class AppButton : CustomButton {
13
13
/// Default content insets based on current trait collection.
14
- var defaultContentInsets : UIEdgeInsets {
14
+ var defaultContentInsets : NSDirectionalEdgeInsets {
15
15
switch traitCollection. userInterfaceIdiom {
16
16
case . phone:
17
- return UIEdgeInsets ( top: 10 , left : 10 , bottom: 10 , right : 10 )
17
+ return NSDirectionalEdgeInsets ( top: 10 , leading : 10 , bottom: 10 , trailing : 10 )
18
18
case . pad:
19
- return UIEdgeInsets ( top: 15 , left : 15 , bottom: 15 , right : 15 )
19
+ return NSDirectionalEdgeInsets ( top: 15 , leading : 15 , bottom: 15 , trailing : 15 )
20
20
default :
21
21
return . zero
22
22
}
@@ -87,30 +87,6 @@ class AppButton: CustomButton {
87
87
}
88
88
}
89
89
90
- /// Prevents updating `contentEdgeInsets` on changes to trait collection.
91
- var overrideContentEdgeInsets = false
92
-
93
- override var contentEdgeInsets : UIEdgeInsets {
94
- didSet {
95
- // Reset inner directional insets when contentEdgeInsets are set directly.
96
- innerDirectionalContentEdgeInsets = nil
97
- }
98
- }
99
-
100
- /// Directional content edge insets that are automatically translated into `contentEdgeInsets` immeditely upon updating the property and on trait collection
101
- /// changes.
102
- var directionalContentEdgeInsets : NSDirectionalEdgeInsets {
103
- get {
104
- innerDirectionalContentEdgeInsets ?? contentEdgeInsets. toDirectionalInsets
105
- }
106
- set {
107
- innerDirectionalContentEdgeInsets = newValue
108
- updateContentEdgeInsetsFromDirectional ( )
109
- }
110
- }
111
-
112
- private var innerDirectionalContentEdgeInsets : NSDirectionalEdgeInsets ?
113
-
114
90
init ( style: Style ) {
115
91
self . style = style
116
92
super. init ( frame: . zero)
@@ -128,32 +104,31 @@ class AppButton: CustomButton {
128
104
}
129
105
130
106
private func commonInit( ) {
131
- super. contentEdgeInsets = defaultContentInsets
132
- imageAlignment = . trailingFixed
133
-
134
- titleLabel? . font = UIFont . systemFont ( ofSize: 18 , weight: . semibold)
135
-
136
- let states : [ UIControl . State ] = [ . normal, . highlighted, . disabled]
137
- states. forEach { state in
138
- if let titleColor = state. customButtonTitleColor {
139
- setTitleColor ( titleColor, for: state)
107
+ imageAlignment = . trailing
108
+ titleAlignment = . leading
109
+
110
+ var config = super. configuration ?? . plain( )
111
+ config. title = title ( for: state)
112
+ config. contentInsets = defaultContentInsets
113
+ config. background. image = style. backgroundImage
114
+ config. titleTextAttributesTransformer =
115
+ UIConfigurationTextAttributesTransformer { [ weak self] attributeContainer in
116
+ guard let self = self else { return attributeContainer }
117
+ var updatedAttributeContainer = attributeContainer
118
+ updatedAttributeContainer. font = UIFont . systemFont ( ofSize: 18 , weight: . semibold)
119
+ updatedAttributeContainer. foregroundColor = self . state. customButtonTitleColor
120
+ return updatedAttributeContainer
140
121
}
141
- }
142
122
143
- // Avoid setting the background image if it's already set via Interface Builder
144
- if backgroundImage ( for: . normal) == nil {
145
- updateButtonBackground ( )
123
+ let configurationHandler : UIButton . ConfigurationUpdateHandler = { button in
124
+ button. alpha = button. state == . disabled ? 0.5 : 1.0
146
125
}
126
+ self . configuration = config
127
+ self . configurationUpdateHandler = configurationHandler
147
128
}
148
129
149
130
/// Set background image based on current style.
150
131
private func updateButtonBackground( ) {
151
- setBackgroundImage ( style. backgroundImage, for: . normal)
152
- }
153
-
154
- /// Update content edge insets from directional edge insets if set.
155
- private func updateContentEdgeInsetsFromDirectional( ) {
156
- guard let directionalEdgeInsets = innerDirectionalContentEdgeInsets else { return }
157
- super. contentEdgeInsets = directionalEdgeInsets. toEdgeInsets ( effectiveUserInterfaceLayoutDirection)
132
+ self . configuration? . background. image = style. backgroundImage
158
133
}
159
134
}
0 commit comments