-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSTTabBarItemModel.swift
More file actions
355 lines (326 loc) · 12.7 KB
/
Copy pathSTTabBarItemModel.swift
File metadata and controls
355 lines (326 loc) · 12.7 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//
// STTabBarItemModel.swift
// STBaseProject
//
// Created by 寒江孤影 on 2024/01/01.
//
import UIKit
/// TabBar Item 显示模式
public enum STTabBarItemDisplayMode: Equatable {
case imageOnly
case textOnly
case imageAndText
case custom
case irregular
}
/// 文案与字体
public struct STTabBarItemTypography: Equatable {
public var fontSize: CGFloat
public var fontName: String
public var isLocalized: Bool
public init(fontSize: CGFloat = 12.0, fontName: String = "PingFangSC-Regular", isLocalized: Bool = false) {
self.fontSize = fontSize
self.fontName = fontName
self.isLocalized = isLocalized
}
public static let standard = STTabBarItemTypography(fontSize: 12.0, fontName: "PingFangSC-Regular", isLocalized: false)
}
/// 文本与背景色(选中 / 未选中)
public struct STTabBarItemColors: Equatable {
public var normalText: UIColor
public var selectedText: UIColor
public var normalBackground: UIColor
public var selectedBackground: UIColor
public init(
normalText: UIColor = .systemGray,
selectedText: UIColor = .systemBlue,
normalBackground: UIColor = .clear,
selectedBackground: UIColor = .clear
) {
self.normalText = normalText
self.selectedText = selectedText
self.normalBackground = normalBackground
self.selectedBackground = selectedBackground
}
public static let standard = STTabBarItemColors()
}
/// 图标区域布局(图文、不规则等共用)
public struct STTabBarItemLayout: Equatable {
public var imageSize: CGSize?
public var imageTopInset: CGFloat
public init(imageSize: CGSize? = nil, imageTopInset: CGFloat = 6.0) {
self.imageSize = imageSize
self.imageTopInset = imageTopInset
}
public static let standard = STTabBarItemLayout(imageSize: nil, imageTopInset: 6.0)
}
/// 徽标
public struct STTabBarItemBadge: Equatable {
public var count: Int
public var backgroundColor: UIColor
public var textColor: UIColor
public init(count: Int = 0, backgroundColor: UIColor = .systemRed, textColor: UIColor = .white) {
self.count = count
self.backgroundColor = backgroundColor
self.textColor = textColor
}
public static let standard = STTabBarItemBadge()
}
/// 凸起(不规则)按钮专属外观;仅在 `displayMode == .irregular` 时参与布局与绘制
public struct STTabBarIrregularStyle: Equatable {
public var protrusionHeight: CGFloat
public var cornerRadius: CGFloat
public var backgroundColor: UIColor
public var shadowColor: UIColor
public var shadowOffset: CGSize
public var shadowRadius: CGFloat
public init(
protrusionHeight: CGFloat = 20.0,
cornerRadius: CGFloat = 25.0,
backgroundColor: UIColor = .systemBlue,
shadowColor: UIColor = .black,
shadowOffset: CGSize = CGSize(width: 0, height: 2),
shadowRadius: CGFloat = 4.0
) {
self.protrusionHeight = protrusionHeight
self.cornerRadius = cornerRadius
self.backgroundColor = backgroundColor
self.shadowColor = shadowColor
self.shadowOffset = shadowOffset
self.shadowRadius = shadowRadius
}
public static let standard = STTabBarIrregularStyle()
}
/// 单个 Tab 项数据:内容 + 分组样式;`isIrregular` 由 `displayMode == .irregular` 推导,避免与枚举重复维护
public struct STTabBarItemModel {
public var title: String
public var normalImage: UIImage?
public var selectedImage: UIImage?
public var normalImageName: String?
public var selectedImageName: String?
public var customView: UIView?
public var displayMode: STTabBarItemDisplayMode
public var colors: STTabBarItemColors
public var typography: STTabBarItemTypography
public var layout: STTabBarItemLayout
public var badge: STTabBarItemBadge
public var irregular: STTabBarIrregularStyle?
public var isEnabled: Bool
public var isIrregular: Bool { displayMode == .irregular }
public init(
title: String = "",
normalImage: UIImage? = nil,
selectedImage: UIImage? = nil,
customView: UIView? = nil,
displayMode: STTabBarItemDisplayMode = .imageAndText,
colors: STTabBarItemColors = .standard,
typography: STTabBarItemTypography = .standard,
layout: STTabBarItemLayout = .standard,
badge: STTabBarItemBadge = .standard,
irregular: STTabBarIrregularStyle? = nil,
isEnabled: Bool = true
) {
self.title = title
self.normalImage = normalImage
self.selectedImage = selectedImage
self.customView = customView
self.displayMode = displayMode
self.colors = colors
self.typography = typography
self.layout = layout
self.badge = badge
self.irregular = irregular
self.isEnabled = isEnabled
}
public init(title: String, normalImage: UIImage? = nil, selectedImage: UIImage? = nil) {
self.init(
title: title,
normalImage: normalImage,
selectedImage: selectedImage,
customView: nil,
displayMode: .imageAndText,
colors: .standard,
typography: .standard,
layout: .standard,
badge: .standard,
irregular: nil,
isEnabled: true
)
}
public static func createWithImageNames(
title: String,
normalImageName: String,
selectedImageName: String,
titleSize: CGFloat = 12.0,
titleFontName: String = "PingFangSC-Regular",
normalTitleColor: UIColor = .systemGray,
selectedTitleColor: UIColor = .systemBlue,
backgroundColor: UIColor = .clear,
imageSize: CGSize? = nil,
imageTopInset: CGFloat = 6.0,
badgeValue: String? = nil,
badgeColor: UIColor = .systemRed,
isLocalized: Bool = false
) -> STTabBarItemModel {
let finalTitle = isLocalized ? title.localized : title
let normalImage = loadImage(named: normalImageName, imageSize: imageSize)
let selectedImage = loadImage(named: selectedImageName, imageSize: imageSize)
let count = badgeCountFromString(badgeValue)
var model = STTabBarItemModel(
title: finalTitle,
normalImage: normalImage,
selectedImage: selectedImage,
displayMode: .imageAndText,
colors: STTabBarItemColors(
normalText: normalTitleColor,
selectedText: selectedTitleColor,
normalBackground: backgroundColor,
selectedBackground: backgroundColor
),
typography: STTabBarItemTypography(fontSize: titleSize, fontName: titleFontName, isLocalized: isLocalized),
layout: STTabBarItemLayout(imageSize: imageSize, imageTopInset: imageTopInset),
badge: STTabBarItemBadge(count: count, backgroundColor: badgeColor, textColor: .white)
)
model.normalImageName = normalImageName
model.selectedImageName = selectedImageName
return model
}
public static func createLocalized(
localizedTitle: String,
normalImageName: String,
selectedImageName: String,
normalColor: UIColor = .systemGray,
selectedColor: UIColor = .systemBlue
) -> STTabBarItemModel {
createWithImageNames(
title: localizedTitle,
normalImageName: normalImageName,
selectedImageName: selectedImageName,
normalTitleColor: normalColor,
selectedTitleColor: selectedColor,
isLocalized: true
)
}
public static func fromUITabBarItem(_ tabBarItem: UITabBarItem) -> STTabBarItemModel {
STTabBarItemModel(
title: tabBarItem.title ?? "",
normalImage: tabBarItem.image,
selectedImage: tabBarItem.selectedImage,
displayMode: .imageAndText,
colors: .standard,
typography: .standard,
badge: STTabBarItemBadge(
count: badgeCountFromString(tabBarItem.badgeValue),
backgroundColor: tabBarItem.badgeColor ?? .systemRed,
textColor: .white
)
)
}
public static func createImageOnly(
normalImageName: String,
selectedImageName: String,
imageSize: CGSize = CGSize(width: 24, height: 24),
backgroundColor: UIColor = .clear,
badgeValue: String? = nil
) -> STTabBarItemModel {
let normalImage = loadImage(named: normalImageName, imageSize: imageSize)
let selectedImage = loadImage(named: selectedImageName, imageSize: imageSize)
let count = badgeCountFromString(badgeValue)
var model = STTabBarItemModel(
title: "",
normalImage: normalImage,
selectedImage: selectedImage,
displayMode: .imageOnly,
colors: STTabBarItemColors(
normalText: .systemGray,
selectedText: .systemBlue,
normalBackground: backgroundColor,
selectedBackground: backgroundColor
),
typography: .standard,
layout: STTabBarItemLayout(imageSize: imageSize, imageTopInset: 6.0),
badge: STTabBarItemBadge(count: count)
)
model.normalImageName = normalImageName
model.selectedImageName = selectedImageName
return model
}
public static func createTextOnly(
title: String,
titleSize: CGFloat = 14.0,
titleFontName: String = "PingFangSC-Medium",
normalTitleColor: UIColor = .systemGray,
selectedTitleColor: UIColor = .systemBlue,
backgroundColor: UIColor = .clear,
isLocalized: Bool = false
) -> STTabBarItemModel {
let finalTitle = isLocalized ? title.localized : title
return STTabBarItemModel(
title: finalTitle,
displayMode: .textOnly,
colors: STTabBarItemColors(
normalText: normalTitleColor,
selectedText: selectedTitleColor,
normalBackground: backgroundColor,
selectedBackground: backgroundColor
),
typography: STTabBarItemTypography(fontSize: titleSize, fontName: titleFontName, isLocalized: isLocalized)
)
}
public static func createIrregular(
title: String,
normalImageName: String,
selectedImageName: String,
irregularHeight: CGFloat = 20.0,
irregularCornerRadius: CGFloat = 25.0,
irregularBackgroundColor: UIColor = .systemBlue,
titleSize: CGFloat = 12.0,
imageSize: CGSize = CGSize(width: 24, height: 24)
) -> STTabBarItemModel {
let normalImage = loadImage(named: normalImageName, imageSize: imageSize)
let selectedImage = loadImage(named: selectedImageName, imageSize: imageSize)
let irr = STTabBarIrregularStyle(
protrusionHeight: irregularHeight,
cornerRadius: irregularCornerRadius,
backgroundColor: irregularBackgroundColor,
shadowColor: .black,
shadowOffset: CGSize(width: 0, height: 2),
shadowRadius: 4.0
)
var model = STTabBarItemModel(
title: title,
normalImage: normalImage,
selectedImage: selectedImage,
displayMode: .irregular,
colors: STTabBarItemColors(
normalText: .white,
selectedText: .white,
normalBackground: irregularBackgroundColor,
selectedBackground: irregularBackgroundColor
),
typography: STTabBarItemTypography(fontSize: titleSize, fontName: "PingFangSC-Regular", isLocalized: false),
layout: STTabBarItemLayout(imageSize: imageSize, imageTopInset: 6.0),
irregular: irr
)
model.normalImageName = normalImageName
model.selectedImageName = selectedImageName
return model
}
public static func createCustom(customView: UIView, title: String = "") -> STTabBarItemModel {
STTabBarItemModel(title: title, customView: customView, displayMode: .custom)
}
private static func loadImage(named imageName: String, imageSize: CGSize? = nil) -> UIImage? {
guard let image = UIImage(named: imageName) else {
STLog("⚠️ STTabBarItemModel: 图片加载失败 - \(imageName)")
return nil
}
return image.withRenderingMode(.alwaysOriginal)
}
private static func badgeCountFromString(_ badgeValue: String?) -> Int {
guard let badgeValue = badgeValue else { return 0 }
if badgeValue == "99+" {
return 100
}
return Int(badgeValue) ?? 0
}
}