mirror of https://github.com/xaoxuu/ProHUD
自定义标签样式
This commit is contained in:
parent
7e672707cf
commit
398a9f8472
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
}()
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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?
|
||||
|
|
|
@ -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)?
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}()
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue