From 398a9f8472dd16bce581f03db2b0a828d01ad0e4 Mon Sep 17 00:00:00 2001 From: xaoxuu Date: Thu, 27 Apr 2023 19:12:48 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHDemo/PHDemo/AlertVC.swift | 16 +++++-- PHDemo/PHDemo/ToastVC.swift | 12 +++-- Sources/ProHUD/Alert/Alert.swift | 8 ++-- Sources/ProHUD/Alert/AlertButton.swift | 7 +++ Sources/ProHUD/Alert/AlertDefaultLayout.swift | 12 ++--- .../ProHUD/Core/Models/Configuration.swift | 46 ++++++++++++------- Sources/ProHUD/Core/Views/Button.swift | 2 +- Sources/ProHUD/Sheet/SheetButton.swift | 7 +++ Sources/ProHUD/Sheet/SheetConfiguration.swift | 19 ++------ .../ProHUD/Sheet/SheetConvenienceLayout.swift | 10 ++-- Sources/ProHUD/Toast/Toast.swift | 8 ++-- Sources/ProHUD/Toast/ToastButton.swift | 7 +++ Sources/ProHUD/Toast/ToastConfiguration.swift | 12 ----- Sources/ProHUD/Toast/ToastDefaultLayout.swift | 12 ++--- 14 files changed, 103 insertions(+), 75 deletions(-) diff --git a/PHDemo/PHDemo/AlertVC.swift b/PHDemo/PHDemo/AlertVC.swift index f090a6b..72948b9 100644 --- a/PHDemo/PHDemo/AlertVC.swift +++ b/PHDemo/PHDemo/AlertVC.swift @@ -71,10 +71,14 @@ class AlertVC: ListVC { list.add(title: "文字 + 按钮") { section in section.add(title: "只有一小段文字 + 无背景色按钮") { Alert { alert in - alert.config.boldTextFont = .systemFont(ofSize: 15) - alert.config.buttonFont = .systemFont(ofSize: 15) alert.config.cardCornerRadius = 12 alert.vm.title = "你正在使用移动网络观看" + alert.config.customTextLabel { label in + label.font = .systemFont(ofSize: 15) + } + alert.config.customButton { button in + button.titleLabel?.font = .systemFont(ofSize: 15) + } } .onViewDidLoad { vc in guard let alert = vc as? Alert else { return @@ -93,10 +97,14 @@ class AlertVC: ListVC { } section.add(title: "只有一段文字 + 无背景色按钮") { Alert { alert in - alert.config.boldTextFont = .systemFont(ofSize: 15) - alert.config.buttonFont = .systemFont(ofSize: 15) alert.config.cardCornerRadius = 12 alert.vm.title = "为了维护社区氛围,上麦用户需进行主播认证" + alert.config.customBodyLabel { label in + label.font = .systemFont(ofSize: 15) + } + alert.config.customButton { button in + button.titleLabel?.font = .systemFont(ofSize: 15) + } } .onViewDidLoad { vc in guard let alert = vc as? Alert else { return diff --git a/PHDemo/PHDemo/ToastVC.swift b/PHDemo/PHDemo/ToastVC.swift index cf48922..0023414 100644 --- a/PHDemo/PHDemo/ToastVC.swift +++ b/PHDemo/PHDemo/ToastVC.swift @@ -120,8 +120,10 @@ class ToastVC: ListVC { mask.backgroundColor = .systemRed } alert.config.iconSize = .init(width: 64, height: 64) - alert.config.boldTextFont = .init(name: "Papyrus", size: 40) alert.config.dynamicTextColor = .white + alert.config.customBodyLabel { label in + label.font = .init(name: "Papyrus", size: 40) + } } } toast.add(action: "同意") { toast in @@ -298,14 +300,18 @@ class ToastVC: ListVC { toast.vm.message = "建议在App启动后进行通用配置设置,所有实例都会先拉取通用配置为默认值,修改这些配置会影响到所有实例。" toast.add(action: "默认", style: .gray) { toast in Toast.Configuration.shared { config in - config.titleFont = .systemFont(ofSize: 19, weight: .bold) + config.customTitleLabel { titleLabel in + titleLabel.font = .systemFont(ofSize: 19, weight: .bold) + } } toast.pop() foo() } toast.add(action: "大号标题") { toast in Toast.Configuration.shared { config in - config.titleFont = .systemFont(ofSize: 28, weight: .medium) + config.customTitleLabel { titleLabel in + titleLabel.font = .systemFont(ofSize: 28, weight: .medium) + } } toast.pop() foo() diff --git a/Sources/ProHUD/Alert/Alert.swift b/Sources/ProHUD/Alert/Alert.swift index a6edc92..0d31837 100644 --- a/Sources/ProHUD/Alert/Alert.swift +++ b/Sources/ProHUD/Alert/Alert.swift @@ -45,9 +45,9 @@ public class Alert: ProHUD.Controller { public lazy var titleLabel: UILabel = { let lb = UILabel() lb.textColor = config.primaryLabelColor - lb.font = config.titleFontByDefault + lb.font = .boldSystemFont(ofSize: 20) lb.textAlignment = .left - lb.numberOfLines = config.titleMaxLines + lb.numberOfLines = 5 return lb }() @@ -55,9 +55,9 @@ public class Alert: ProHUD.Controller { public lazy var bodyLabel: UILabel = { let lb = UILabel() lb.textColor = config.primaryLabelColor - lb.font = config.bodyFontByDefault + lb.font = .systemFont(ofSize: 17) lb.textAlignment = .left - lb.numberOfLines = config.bodyMaxLines + lb.numberOfLines = 20 return lb }() diff --git a/Sources/ProHUD/Alert/AlertButton.swift b/Sources/ProHUD/Alert/AlertButton.swift index 418c1af..52f7ca1 100644 --- a/Sources/ProHUD/Alert/AlertButton.swift +++ b/Sources/ProHUD/Alert/AlertButton.swift @@ -8,9 +8,16 @@ import UIKit public class AlertButton: Button { + override var customEdgeInset: UIEdgeInsets { .init(top: 12, left: 24, bottom: 12, right: 24) } + + public override func update(config: ProHUD.Configuration, action: Action) { + titleLabel?.font = .boldSystemFont(ofSize: 15) + super.update(config: config, action: action) + } + } diff --git a/Sources/ProHUD/Alert/AlertDefaultLayout.swift b/Sources/ProHUD/Alert/AlertDefaultLayout.swift index af03c9d..1fccdfb 100644 --- a/Sources/ProHUD/Alert/AlertDefaultLayout.swift +++ b/Sources/ProHUD/Alert/AlertDefaultLayout.swift @@ -189,11 +189,9 @@ extension Alert { textStack.insertArrangedSubview(titleLabel, at: 0) } if bodyCount > 0 { - // 有message - titleLabel.font = config.titleFontByDefault + config.customTitleLabel?(titleLabel) } else { - // 没有message - titleLabel.font = config.boldTextFontByDefault + config.customTextLabel?(titleLabel) } } else { if textStack.arrangedSubviews.contains(titleLabel) { @@ -207,11 +205,9 @@ extension Alert { textStack.addArrangedSubview(bodyLabel) } if titleCount > 0 { - // 有title - bodyLabel.font = config.bodyFontByDefault + config.customBodyLabel?(bodyLabel) } else { - // 没有title - bodyLabel.font = config.boldTextFontByDefault + config.customTextLabel?(bodyLabel) } } else { if textStack.arrangedSubviews.contains(bodyLabel) { diff --git a/Sources/ProHUD/Core/Models/Configuration.swift b/Sources/ProHUD/Core/Models/Configuration.swift index 95ef404..8a1548d 100644 --- a/Sources/ProHUD/Core/Models/Configuration.swift +++ b/Sources/ProHUD/Core/Models/Configuration.swift @@ -107,28 +107,42 @@ public class Configuration: NSObject { public var iconSize = CGSize(width: 44, height: 44) // MARK: 文本样式 - /// 标题字体 - public var titleFont: UIFont? - var titleFontByDefault: UIFont { titleFont ?? .boldSystemFont(ofSize: 20) } - /// 标题最多行数 - public var titleMaxLines = Int(5) + var customTextLabel: ((_ label: UILabel) -> Void)? = { label in + label.font = .boldSystemFont(ofSize: 18) + } - /// 加粗字体(如果只有标题或者只有正文,则显示这种字体) - public var boldTextFont: UIFont? - var boldTextFontByDefault: UIFont { boldTextFont ?? .boldSystemFont(ofSize: 18) } + /// 自定义文本标签(标题或正文) + /// - Parameter handler: 自定义文本标签(标题或正文) + public func customTextLabel(handler: @escaping (_ label: UILabel) -> Void) { + customTextLabel = handler + } - /// 正文字体 - public var bodyFont: UIFont? - var bodyFontByDefault: UIFont { bodyFont ?? .systemFont(ofSize: 17) } + var customTitleLabel: ((_ label: UILabel) -> Void)? - /// 正文最多行数 - public var bodyMaxLines = Int(20) + /// 自定义标题标签 + /// - Parameter handler: 自定义标题标签 + public func customTitleLabel(handler: @escaping (_ label: UILabel) -> Void) { + customTitleLabel = handler + } + + /// 正文 + var customBodyLabel: ((_ label: UILabel) -> Void)? + + /// 自定义正文标签 + /// - Parameter handler: 自定义正文标签 + public func customBodyLabel(handler: @escaping (_ label: UILabel) -> Void) { + customBodyLabel = handler + } // MARK: 按钮样式 - /// 按钮字体 - public var buttonFont: UIFont? - var buttonFontByDefault: UIFont { buttonFont ?? .boldSystemFont(ofSize: 15) } + var customButton: ((_ button: Button) -> Void)? + + /// 自定义按钮 + /// - Parameter handler: 自定义按钮 + public func customButton(handler: @escaping (_ button: Button) -> Void) { + customButton = handler + } /// 按钮圆角 public var buttonCornerRadius: CGFloat? diff --git a/Sources/ProHUD/Core/Views/Button.swift b/Sources/ProHUD/Core/Views/Button.swift index 15b0d36..287642e 100644 --- a/Sources/ProHUD/Core/Views/Button.swift +++ b/Sources/ProHUD/Core/Views/Button.swift @@ -37,7 +37,6 @@ open class Button: UIButton { /// 更新按钮 /// - Parameter style: 样式 open func update(config: ProHUD.Configuration, action: Action) { - titleLabel?.font = config.buttonFontByDefault layer.cornerRadiusWithContinuous = config.buttonCornerRadiusByDefault self.action = action setTitle(action.title, for: .normal) @@ -63,6 +62,7 @@ open class Button: UIButton { contentEdgeInsets.top = 0 contentEdgeInsets.bottom = 0 } + config.customButton?(self) } var onTouchDown: ((_ action: Button) -> Void)? diff --git a/Sources/ProHUD/Sheet/SheetButton.swift b/Sources/ProHUD/Sheet/SheetButton.swift index 0aaa69c..659d0a3 100644 --- a/Sources/ProHUD/Sheet/SheetButton.swift +++ b/Sources/ProHUD/Sheet/SheetButton.swift @@ -8,7 +8,14 @@ import UIKit public class SheetButton: Button { + override var customEdgeInset: UIEdgeInsets { .init(top: 14, left: 28, bottom: 14, right: 28) } + + public override func update(config: ProHUD.Configuration, action: Action) { + titleLabel?.font = .boldSystemFont(ofSize: 18) + super.update(config: config, action: action) + } + } diff --git a/Sources/ProHUD/Sheet/SheetConfiguration.swift b/Sources/ProHUD/Sheet/SheetConfiguration.swift index 22635d4..6b91b6f 100644 --- a/Sources/ProHUD/Sheet/SheetConfiguration.swift +++ b/Sources/ProHUD/Sheet/SheetConfiguration.swift @@ -18,9 +18,11 @@ public extension Sheet { public var isFullScreen = false /// 副标题字体 - public var subTitleFont: UIFont? - var subTitleFontByDefault: UIFont { - subTitleFont ?? .systemFont(ofSize: 20, weight: .bold) + + var customSubtitleLabel: ((_ label: UILabel) -> Void)? + + public func customSubtitleLabel(handler: @escaping (_ label: UILabel) -> Void) { + customSubtitleLabel = handler } static var customShared: ((_ config: Configuration) -> Void)? @@ -43,17 +45,6 @@ public extension Sheet { override var cardMaxHeightByDefault: CGFloat { cardMaxHeight ?? (UIScreen.main.bounds.height - 50) } - override var titleFontByDefault: UIFont { - titleFont ?? .systemFont(ofSize: 24, weight: .bold) - } - - override var bodyFontByDefault: UIFont { - bodyFont ?? .systemFont(ofSize: 16, weight: .regular) - } - - override var buttonFontByDefault: UIFont { - buttonFont ?? .systemFont(ofSize: 18, weight: .bold) - } override var buttonCornerRadiusByDefault: CGFloat { buttonCornerRadius ?? 12 } override var animateDurationForBuildInByDefault: CGFloat { diff --git a/Sources/ProHUD/Sheet/SheetConvenienceLayout.swift b/Sources/ProHUD/Sheet/SheetConvenienceLayout.swift index 65e5d75..04642a8 100644 --- a/Sources/ProHUD/Sheet/SheetConvenienceLayout.swift +++ b/Sources/ProHUD/Sheet/SheetConvenienceLayout.swift @@ -132,8 +132,10 @@ public extension Sheet { /// - Parameter text: 文本 @discardableResult func add(title text: String?) -> UILabel { let lb = add(subTitle: text) - lb.font = config.titleFontByDefault + lb.font = .boldSystemFont(ofSize: 24) + lb.textColor = config.primaryLabelColor lb.textAlignment = .center + config.customTitleLabel?(lb) return lb } @@ -141,10 +143,11 @@ public extension Sheet { /// - Parameter text: 文本 @discardableResult func add(subTitle text: String?) -> UILabel { let lb = UILabel() - lb.font = config.subTitleFontByDefault + lb.font = .boldSystemFont(ofSize: 20) lb.textColor = config.primaryLabelColor lb.numberOfLines = 0 lb.textAlignment = .justified + config.customSubtitleLabel?(lb) lb.text = text contentStack.addArrangedSubview(lb) if #available(iOS 11.0, *) { @@ -165,10 +168,11 @@ public extension Sheet { /// - Parameter text: 文本 @discardableResult func add(message text: String?) -> UILabel { let lb = UILabel() - lb.font = config.bodyFontByDefault + lb.font = .systemFont(ofSize: 16) lb.textColor = config.primaryLabelColor lb.numberOfLines = 0 lb.textAlignment = .justified + config.customTitleLabel?(lb) lb.text = text contentStack.addArrangedSubview(lb) return lb diff --git a/Sources/ProHUD/Toast/Toast.swift b/Sources/ProHUD/Toast/Toast.swift index a42412a..eab521a 100644 --- a/Sources/ProHUD/Toast/Toast.swift +++ b/Sources/ProHUD/Toast/Toast.swift @@ -60,9 +60,9 @@ public class Toast: Controller { public lazy var titleLabel: UILabel = { let lb = UILabel() lb.textColor = config.primaryLabelColor - lb.font = config.titleFontByDefault + lb.font = .boldSystemFont(ofSize: 18) lb.textAlignment = .justified - lb.numberOfLines = config.titleMaxLines + lb.numberOfLines = 5 return lb }() @@ -70,9 +70,9 @@ public class Toast: Controller { public lazy var bodyLabel: UILabel = { let lb = UILabel() lb.textColor = config.primaryLabelColor - lb.font = config.bodyFontByDefault + lb.font = .systemFont(ofSize: 16) lb.textAlignment = .justified - lb.numberOfLines = config.bodyMaxLines + lb.numberOfLines = 20 return lb }() diff --git a/Sources/ProHUD/Toast/ToastButton.swift b/Sources/ProHUD/Toast/ToastButton.swift index 11a9a25..3602241 100644 --- a/Sources/ProHUD/Toast/ToastButton.swift +++ b/Sources/ProHUD/Toast/ToastButton.swift @@ -8,8 +8,15 @@ import UIKit public class ToastButton: Button { + override var customEdgeInset: UIEdgeInsets { .init(top: 10, left: 24, bottom: 10, right: 24) } + + public override func update(config: ProHUD.Configuration, action: Action) { + titleLabel?.font = .boldSystemFont(ofSize: 15) + super.update(config: config, action: action) + } + } diff --git a/Sources/ProHUD/Toast/ToastConfiguration.swift b/Sources/ProHUD/Toast/ToastConfiguration.swift index 978c896..f661b6d 100644 --- a/Sources/ProHUD/Toast/ToastConfiguration.swift +++ b/Sources/ProHUD/Toast/ToastConfiguration.swift @@ -30,18 +30,6 @@ public extension Toast { cardMaxHeight ?? (UIScreen.main.bounds.height / 3) } - override var titleFontByDefault: UIFont { - titleFont ?? .systemFont(ofSize: 18, weight: .bold) - } - - override var boldTextFontByDefault: UIFont { - boldTextFont ?? .systemFont(ofSize: 16, weight: .bold) - } - - override var bodyFontByDefault: UIFont { - bodyFont ?? .systemFont(ofSize: 16, weight: .regular) - } - override var animateDurationForBuildInByDefault: CGFloat { animateDurationForBuildIn ?? 0.8 } diff --git a/Sources/ProHUD/Toast/ToastDefaultLayout.swift b/Sources/ProHUD/Toast/ToastDefaultLayout.swift index 110a96a..81bf152 100644 --- a/Sources/ProHUD/Toast/ToastDefaultLayout.swift +++ b/Sources/ProHUD/Toast/ToastDefaultLayout.swift @@ -45,10 +45,10 @@ extension Toast: DefaultLayout { let bodyCount = vm.message?.count ?? 0 if titleCount > 0 { textStack.insertArrangedSubview(titleLabel, at: 0) - if bodyCount == 0 { - titleLabel.font = config.boldTextFontByDefault + if bodyCount > 0 { + config.customTitleLabel?(titleLabel) } else { - titleLabel.font = config.titleFontByDefault + config.customTextLabel?(bodyLabel) } } else { if textStack.arrangedSubviews.contains(titleLabel) { @@ -58,10 +58,10 @@ extension Toast: DefaultLayout { } if bodyCount > 0 { textStack.addArrangedSubview(bodyLabel) - if titleCount == 0 { - bodyLabel.font = config.boldTextFontByDefault + if titleCount > 0 { + config.customBodyLabel?(bodyLabel) } else { - bodyLabel.font = config.bodyFontByDefault + config.customTextLabel?(bodyLabel) } } else { if textStack.arrangedSubviews.contains(bodyLabel) {