-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSTIconBtn.swift
More file actions
223 lines (197 loc) · 6.57 KB
/
Copy pathSTIconBtn.swift
File metadata and controls
223 lines (197 loc) · 6.57 KB
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
//
// STIconBtn.swift
// STBaseProject
//
// Created by 寒江孤影 on 2019/10/14.
//
import UIKit
public enum STIconPosition: Int {
case left = 0
case right = 1
case top = 2
case bottom = 3
}
public class STIconBtnBuilder {
private weak var button: STIconBtn?
init(button: STIconBtn) {
self.button = button
}
/// 设置图标位置
/// - Parameter position: 图标位置
/// - Returns: 构建器实例,支持链式调用
@discardableResult
public func iconPosition(_ position: STIconPosition) -> STIconBtnBuilder {
self.button?.iconPosition = position
return self
}
/// 设置图标和文字之间的间距
/// - Parameter spacing: 间距值
/// - Returns: 构建器实例,支持链式调用
@discardableResult
public func spacing(_ spacing: CGFloat) -> STIconBtnBuilder {
self.button?.spacing = spacing
return self
}
/// 设置按钮内容边距
@discardableResult
public func contentInsets(_ insets: UIEdgeInsets) -> STIconBtnBuilder {
self.button?.iconContentInsets = insets
return self
}
/// 完成配置,执行布局更新
/// 使用示例:
/// ```
/// button.configure()
/// .iconPosition(.right)
/// .spacing(12)
/// .done()
/// ```
public func done() {
self.button?.setNeedsLayout()
}
}
// MARK: - 图标按钮类
@IBDesignable
open class STIconBtn: STBtn {
/// 图标位置
public var iconPosition: STIconPosition = .left {
didSet {
guard oldValue != iconPosition else { return }
self.invalidateIntrinsicContentSize()
self.setNeedsUpdateConfiguration()
}
}
/// 图标和文字之间的间距
public var spacing: CGFloat = 8 {
didSet {
guard oldValue != spacing else { return }
self.invalidateIntrinsicContentSize()
self.setNeedsUpdateConfiguration()
}
}
/// 图文整体内容边距
public var iconContentInsets: UIEdgeInsets = .zero {
didSet {
guard oldValue != iconContentInsets else { return }
self.invalidateIntrinsicContentSize()
self.setNeedsUpdateConfiguration()
}
}
/// xib 中配置图标位置:0 左、1 右、2 上、3 下
@IBInspectable open var iconPositionRaw: Int {
get {
return self.iconPosition.rawValue
}
set {
guard let position = STIconPosition(rawValue: newValue) else { return }
self.iconPosition = position
}
}
@IBInspectable open var iconSpacing: CGFloat {
get {
return self.spacing
}
set {
self.spacing = newValue
}
}
@IBInspectable open var iconContentInsetTop: CGFloat {
get {
return self.iconContentInsets.top
}
set {
self.iconContentInsets.top = newValue
}
}
@IBInspectable open var iconContentInsetLeft: CGFloat {
get {
return self.iconContentInsets.left
}
set {
self.iconContentInsets.left = newValue
}
}
@IBInspectable open var iconContentInsetBottom: CGFloat {
get {
return self.iconContentInsets.bottom
}
set {
self.iconContentInsets.bottom = newValue
}
}
@IBInspectable open var iconContentInsetRight: CGFloat {
get {
return self.iconContentInsets.right
}
set {
self.iconContentInsets.right = newValue
}
}
/// 开始链式调用配置
/// 使用示例:
/// ```
/// button.configure()
/// .iconPosition(.right)
/// .spacing(12)
/// .done()
/// ```
/// - Returns: 构建器实例
public func configure() -> STIconBtnBuilder {
return STIconBtnBuilder(button: self)
}
/// 更新布局(当直接设置属性时使用)
/// 使用示例:
/// ```
/// button.iconPosition = .right
/// button.spacing = 12
/// button.updateLayout()
/// ```
public func updateLayout() {
self.invalidateIntrinsicContentSize()
self.setNeedsUpdateConfiguration()
}
// MARK: - 惰性无障碍标签
// 仅当调用方未显式设置 accessibilityLabel 时,才按下述兜底链计算:
// 有 title 用 title;无 title 但有 image 用兜底文案;二者皆无则 nil。
// 采用惰性读取而非在 image/title 赋值时写入,从根本上消除属性赋值顺序依赖。
private var st_explicitAccessibilityLabel: String?
/// 仅含图片时的默认无障碍文案;可设置以区分不同按钮语义(如"返回"/"更多"),亦可被子类重写本地化。
open var st_fallbackAccessibilityLabel: String = "按钮"
override open var accessibilityLabel: String? {
get {
if let explicit = self.st_explicitAccessibilityLabel { return explicit }
if let title = self.currentTitle, !title.isEmpty { return title }
if self.currentImage != nil { return self.st_fallbackAccessibilityLabel }
return nil
}
set {
self.st_explicitAccessibilityLabel = newValue
}
}
override open func refineButtonConfiguration(_ button: UIButton, configuration config: inout UIButton.Configuration) {
super.refineButtonConfiguration(button, configuration: &config)
let icon = self.iconContentInsets
// `iconContentInsets` 以绝对值语义写入 `config.contentInsets`。
// 如需在此之上额外叠加自定义 padding,请通过 `onConfigurationUpdate` 修改传入的 `config.contentInsets`;
// 直接写 `self.configuration?.contentInsets` 会在下一次 update 时被 `iconContentInsets` 覆盖。
config.contentInsets = NSDirectionalEdgeInsets(
top: icon.top,
leading: icon.left,
bottom: icon.bottom,
trailing: icon.right
)
let hasImage = self.currentImage != nil
let hasTitle = !(self.currentTitle?.isEmpty ?? true) || self.currentAttributedTitle != nil
switch self.iconPosition {
case .left:
config.imagePlacement = .leading
case .right:
config.imagePlacement = .trailing
case .top:
config.imagePlacement = .top
case .bottom:
config.imagePlacement = .bottom
}
config.imagePadding = (hasImage && hasTitle) ? self.spacing : 0
}
}