自定义标签样式

This commit is contained in:
xaoxuu 2023-04-27 19:12:48 +08:00
parent 7e672707cf
commit 398a9f8472
14 changed files with 103 additions and 75 deletions

View File

@ -71,10 +71,14 @@ class AlertVC: ListVC {
list.add(title: "文字 + 按钮") { section in list.add(title: "文字 + 按钮") { section in
section.add(title: "只有一小段文字 + 无背景色按钮") { section.add(title: "只有一小段文字 + 无背景色按钮") {
Alert { alert in Alert { alert in
alert.config.boldTextFont = .systemFont(ofSize: 15)
alert.config.buttonFont = .systemFont(ofSize: 15)
alert.config.cardCornerRadius = 12 alert.config.cardCornerRadius = 12
alert.vm.title = "你正在使用移动网络观看" 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 } .onViewDidLoad { vc in
guard let alert = vc as? Alert else { guard let alert = vc as? Alert else {
return return
@ -93,10 +97,14 @@ class AlertVC: ListVC {
} }
section.add(title: "只有一段文字 + 无背景色按钮") { section.add(title: "只有一段文字 + 无背景色按钮") {
Alert { alert in Alert { alert in
alert.config.boldTextFont = .systemFont(ofSize: 15)
alert.config.buttonFont = .systemFont(ofSize: 15)
alert.config.cardCornerRadius = 12 alert.config.cardCornerRadius = 12
alert.vm.title = "为了维护社区氛围,上麦用户需进行主播认证" 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 } .onViewDidLoad { vc in
guard let alert = vc as? Alert else { guard let alert = vc as? Alert else {
return return

View File

@ -120,8 +120,10 @@ class ToastVC: ListVC {
mask.backgroundColor = .systemRed mask.backgroundColor = .systemRed
} }
alert.config.iconSize = .init(width: 64, height: 64) alert.config.iconSize = .init(width: 64, height: 64)
alert.config.boldTextFont = .init(name: "Papyrus", size: 40)
alert.config.dynamicTextColor = .white alert.config.dynamicTextColor = .white
alert.config.customBodyLabel { label in
label.font = .init(name: "Papyrus", size: 40)
}
} }
} }
toast.add(action: "同意") { toast in toast.add(action: "同意") { toast in
@ -298,14 +300,18 @@ class ToastVC: ListVC {
toast.vm.message = "建议在App启动后进行通用配置设置所有实例都会先拉取通用配置为默认值修改这些配置会影响到所有实例。" toast.vm.message = "建议在App启动后进行通用配置设置所有实例都会先拉取通用配置为默认值修改这些配置会影响到所有实例。"
toast.add(action: "默认", style: .gray) { toast in toast.add(action: "默认", style: .gray) { toast in
Toast.Configuration.shared { config 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() toast.pop()
foo() foo()
} }
toast.add(action: "大号标题") { toast in toast.add(action: "大号标题") { toast in
Toast.Configuration.shared { config 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() toast.pop()
foo() foo()

View File

@ -45,9 +45,9 @@ public class Alert: ProHUD.Controller {
public lazy var titleLabel: UILabel = { public lazy var titleLabel: UILabel = {
let lb = UILabel() let lb = UILabel()
lb.textColor = config.primaryLabelColor lb.textColor = config.primaryLabelColor
lb.font = config.titleFontByDefault lb.font = .boldSystemFont(ofSize: 20)
lb.textAlignment = .left lb.textAlignment = .left
lb.numberOfLines = config.titleMaxLines lb.numberOfLines = 5
return lb return lb
}() }()
@ -55,9 +55,9 @@ public class Alert: ProHUD.Controller {
public lazy var bodyLabel: UILabel = { public lazy var bodyLabel: UILabel = {
let lb = UILabel() let lb = UILabel()
lb.textColor = config.primaryLabelColor lb.textColor = config.primaryLabelColor
lb.font = config.bodyFontByDefault lb.font = .systemFont(ofSize: 17)
lb.textAlignment = .left lb.textAlignment = .left
lb.numberOfLines = config.bodyMaxLines lb.numberOfLines = 20
return lb return lb
}() }()

View File

@ -8,9 +8,16 @@
import UIKit import UIKit
public class AlertButton: Button { public class AlertButton: Button {
override var customEdgeInset: UIEdgeInsets { override var customEdgeInset: UIEdgeInsets {
.init(top: 12, left: 24, bottom: 12, right: 24) .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)
}
} }

View File

@ -189,11 +189,9 @@ extension Alert {
textStack.insertArrangedSubview(titleLabel, at: 0) textStack.insertArrangedSubview(titleLabel, at: 0)
} }
if bodyCount > 0 { if bodyCount > 0 {
// message config.customTitleLabel?(titleLabel)
titleLabel.font = config.titleFontByDefault
} else { } else {
// message config.customTextLabel?(titleLabel)
titleLabel.font = config.boldTextFontByDefault
} }
} else { } else {
if textStack.arrangedSubviews.contains(titleLabel) { if textStack.arrangedSubviews.contains(titleLabel) {
@ -207,11 +205,9 @@ extension Alert {
textStack.addArrangedSubview(bodyLabel) textStack.addArrangedSubview(bodyLabel)
} }
if titleCount > 0 { if titleCount > 0 {
// title config.customBodyLabel?(bodyLabel)
bodyLabel.font = config.bodyFontByDefault
} else { } else {
// title config.customTextLabel?(bodyLabel)
bodyLabel.font = config.boldTextFontByDefault
} }
} else { } else {
if textStack.arrangedSubviews.contains(bodyLabel) { if textStack.arrangedSubviews.contains(bodyLabel) {

View File

@ -107,28 +107,42 @@ public class Configuration: NSObject {
public var iconSize = CGSize(width: 44, height: 44) public var iconSize = CGSize(width: 44, height: 44)
// MARK: // MARK:
///
public var titleFont: UIFont?
var titleFontByDefault: UIFont { titleFont ?? .boldSystemFont(ofSize: 20) }
/// var customTextLabel: ((_ label: UILabel) -> Void)? = { label in
public var titleMaxLines = Int(5) label.font = .boldSystemFont(ofSize: 18)
}
/// ///
public var boldTextFont: UIFont? /// - Parameter handler:
var boldTextFontByDefault: UIFont { boldTextFont ?? .boldSystemFont(ofSize: 18) } public func customTextLabel(handler: @escaping (_ label: UILabel) -> Void) {
customTextLabel = handler
}
/// var customTitleLabel: ((_ label: UILabel) -> Void)?
public var bodyFont: UIFont?
var bodyFontByDefault: UIFont { bodyFont ?? .systemFont(ofSize: 17) }
/// ///
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: // MARK:
/// var customButton: ((_ button: Button) -> Void)?
public var buttonFont: UIFont?
var buttonFontByDefault: UIFont { buttonFont ?? .boldSystemFont(ofSize: 15) } ///
/// - Parameter handler:
public func customButton(handler: @escaping (_ button: Button) -> Void) {
customButton = handler
}
/// ///
public var buttonCornerRadius: CGFloat? public var buttonCornerRadius: CGFloat?

View File

@ -37,7 +37,6 @@ open class Button: UIButton {
/// ///
/// - Parameter style: /// - Parameter style:
open func update(config: ProHUD.Configuration, action: Action) { open func update(config: ProHUD.Configuration, action: Action) {
titleLabel?.font = config.buttonFontByDefault
layer.cornerRadiusWithContinuous = config.buttonCornerRadiusByDefault layer.cornerRadiusWithContinuous = config.buttonCornerRadiusByDefault
self.action = action self.action = action
setTitle(action.title, for: .normal) setTitle(action.title, for: .normal)
@ -63,6 +62,7 @@ open class Button: UIButton {
contentEdgeInsets.top = 0 contentEdgeInsets.top = 0
contentEdgeInsets.bottom = 0 contentEdgeInsets.bottom = 0
} }
config.customButton?(self)
} }
var onTouchDown: ((_ action: Button) -> Void)? var onTouchDown: ((_ action: Button) -> Void)?

View File

@ -8,7 +8,14 @@
import UIKit import UIKit
public class SheetButton: Button { public class SheetButton: Button {
override var customEdgeInset: UIEdgeInsets { override var customEdgeInset: UIEdgeInsets {
.init(top: 14, left: 28, bottom: 14, right: 28) .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)
}
} }

View File

@ -18,9 +18,11 @@ public extension Sheet {
public var isFullScreen = false public var isFullScreen = false
/// ///
public var subTitleFont: UIFont?
var subTitleFontByDefault: UIFont { var customSubtitleLabel: ((_ label: UILabel) -> Void)?
subTitleFont ?? .systemFont(ofSize: 20, weight: .bold)
public func customSubtitleLabel(handler: @escaping (_ label: UILabel) -> Void) {
customSubtitleLabel = handler
} }
static var customShared: ((_ config: Configuration) -> Void)? 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 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 buttonCornerRadiusByDefault: CGFloat { buttonCornerRadius ?? 12 }
override var animateDurationForBuildInByDefault: CGFloat { override var animateDurationForBuildInByDefault: CGFloat {

View File

@ -132,8 +132,10 @@ public extension Sheet {
/// - Parameter text: /// - Parameter text:
@discardableResult func add(title text: String?) -> UILabel { @discardableResult func add(title text: String?) -> UILabel {
let lb = add(subTitle: text) let lb = add(subTitle: text)
lb.font = config.titleFontByDefault lb.font = .boldSystemFont(ofSize: 24)
lb.textColor = config.primaryLabelColor
lb.textAlignment = .center lb.textAlignment = .center
config.customTitleLabel?(lb)
return lb return lb
} }
@ -141,10 +143,11 @@ public extension Sheet {
/// - Parameter text: /// - Parameter text:
@discardableResult func add(subTitle text: String?) -> UILabel { @discardableResult func add(subTitle text: String?) -> UILabel {
let lb = UILabel() let lb = UILabel()
lb.font = config.subTitleFontByDefault lb.font = .boldSystemFont(ofSize: 20)
lb.textColor = config.primaryLabelColor lb.textColor = config.primaryLabelColor
lb.numberOfLines = 0 lb.numberOfLines = 0
lb.textAlignment = .justified lb.textAlignment = .justified
config.customSubtitleLabel?(lb)
lb.text = text lb.text = text
contentStack.addArrangedSubview(lb) contentStack.addArrangedSubview(lb)
if #available(iOS 11.0, *) { if #available(iOS 11.0, *) {
@ -165,10 +168,11 @@ public extension Sheet {
/// - Parameter text: /// - Parameter text:
@discardableResult func add(message text: String?) -> UILabel { @discardableResult func add(message text: String?) -> UILabel {
let lb = UILabel() let lb = UILabel()
lb.font = config.bodyFontByDefault lb.font = .systemFont(ofSize: 16)
lb.textColor = config.primaryLabelColor lb.textColor = config.primaryLabelColor
lb.numberOfLines = 0 lb.numberOfLines = 0
lb.textAlignment = .justified lb.textAlignment = .justified
config.customTitleLabel?(lb)
lb.text = text lb.text = text
contentStack.addArrangedSubview(lb) contentStack.addArrangedSubview(lb)
return lb return lb

View File

@ -60,9 +60,9 @@ public class Toast: Controller {
public lazy var titleLabel: UILabel = { public lazy var titleLabel: UILabel = {
let lb = UILabel() let lb = UILabel()
lb.textColor = config.primaryLabelColor lb.textColor = config.primaryLabelColor
lb.font = config.titleFontByDefault lb.font = .boldSystemFont(ofSize: 18)
lb.textAlignment = .justified lb.textAlignment = .justified
lb.numberOfLines = config.titleMaxLines lb.numberOfLines = 5
return lb return lb
}() }()
@ -70,9 +70,9 @@ public class Toast: Controller {
public lazy var bodyLabel: UILabel = { public lazy var bodyLabel: UILabel = {
let lb = UILabel() let lb = UILabel()
lb.textColor = config.primaryLabelColor lb.textColor = config.primaryLabelColor
lb.font = config.bodyFontByDefault lb.font = .systemFont(ofSize: 16)
lb.textAlignment = .justified lb.textAlignment = .justified
lb.numberOfLines = config.bodyMaxLines lb.numberOfLines = 20
return lb return lb
}() }()

View File

@ -8,8 +8,15 @@
import UIKit import UIKit
public class ToastButton: Button { public class ToastButton: Button {
override var customEdgeInset: UIEdgeInsets { override var customEdgeInset: UIEdgeInsets {
.init(top: 10, left: 24, bottom: 10, right: 24) .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)
}
} }

View File

@ -30,18 +30,6 @@ public extension Toast {
cardMaxHeight ?? (UIScreen.main.bounds.height / 3) 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 { override var animateDurationForBuildInByDefault: CGFloat {
animateDurationForBuildIn ?? 0.8 animateDurationForBuildIn ?? 0.8
} }

View File

@ -45,10 +45,10 @@ extension Toast: DefaultLayout {
let bodyCount = vm.message?.count ?? 0 let bodyCount = vm.message?.count ?? 0
if titleCount > 0 { if titleCount > 0 {
textStack.insertArrangedSubview(titleLabel, at: 0) textStack.insertArrangedSubview(titleLabel, at: 0)
if bodyCount == 0 { if bodyCount > 0 {
titleLabel.font = config.boldTextFontByDefault config.customTitleLabel?(titleLabel)
} else { } else {
titleLabel.font = config.titleFontByDefault config.customTextLabel?(bodyLabel)
} }
} else { } else {
if textStack.arrangedSubviews.contains(titleLabel) { if textStack.arrangedSubviews.contains(titleLabel) {
@ -58,10 +58,10 @@ extension Toast: DefaultLayout {
} }
if bodyCount > 0 { if bodyCount > 0 {
textStack.addArrangedSubview(bodyLabel) textStack.addArrangedSubview(bodyLabel)
if titleCount == 0 { if titleCount > 0 {
bodyLabel.font = config.boldTextFontByDefault config.customBodyLabel?(bodyLabel)
} else { } else {
bodyLabel.font = config.bodyFontByDefault config.customTextLabel?(bodyLabel)
} }
} else { } else {
if textStack.arrangedSubviews.contains(bodyLabel) { if textStack.arrangedSubviews.contains(bodyLabel) {