// // DLAlert.swift // Alamofire // // Created by osell on 2023/8/17. // import UIKit public class DLAlert { /// 显示Alert, subTitle是使用文本 /// - Parameters: /// - config: 配置信息 /// - title: 标题 /// - subTitle: 子标题 /// - moreTitle: 更多按钮标题 /// - moreAction: 更多按钮点击回调 /// - defulatTitle: 默认按钮标题 /// - defulatAction: 默认按钮点击回调 public static func show(config: DLAlertConfig = DLAlertConfig(), title: String?=nil, subTitle: String?=nil, content: String?=nil, moreTitle: String?=nil, moreAction: (() -> Void)?=nil, defaultTitle: String, defaultAction: (() -> Void)?=nil) { guard let window = UIApplication.shared.windows.filter({ $0.isKeyWindow }).first else { return } if title?.isEmpty != false && subTitle?.isEmpty != false { return } let view = DLAlertView(title: title, subtitle: subTitle, content:content, moreBtnTitle: moreTitle, defaultBtnTitle: defaultTitle, config: config) view.defaultAction = defaultAction view.moreAction = moreAction view.show(on: window) } /// 显示Alert, subTitle是使用富文本 /// - Parameters: /// - config: 配置信息 /// - title: 标题 /// - subTitle: 子标题,富文本 /// - moreTitle: 更多按钮标题 /// - moreAction: 更多按钮点击回调 /// - defulatTitle: 默认按钮标题 /// - defulatAction: 默认按钮点击回调 public static func showAttributeText(config: DLAlertConfig = DLAlertConfig(), title: String?=nil, subtitle: NSAttributedString?=nil, moreTitle: String?=nil, moreAction: (() -> Void)?=nil, defaultTitle: String, defaultAction: (() -> Void)?=nil) { guard let window = UIApplication.shared.windows.filter({ $0.isKeyWindow }).first else { return } let view = DLAlertView(title: title, subtitleAttribute: subtitle, moreBtnTitle: moreTitle, defaultBtnTitle: defaultTitle, config: config) view.defaultAction = defaultAction view.moreAction = moreAction view.show(on: window) } } public class DLAlertConfig { /// cover背景颜色 public var coverBgColor: UIColor = .black.withAlphaComponent(0.5) /// 内容背景颜色 public var contentBgColor: UIColor = .white /// 标题颜色 public var titleColor: UIColor = ThemeManager.shared.color.titleColor /// 子标题颜色 public var subtitleColor: UIColor = ThemeManager.shared.color.titleColor /// 内容颜色 public var contentColor: UIColor = ThemeManager.shared.color.contentColor /// 标题字体大小 public var titleFont: UIFont = .systemFont(ofSize: 18, weight: .medium) /// 子标题字体大小 public var subtitleFont: UIFont = .systemFont(ofSize: 15, weight: .regular) /// 内容 public var contentFont: UIFont = .systemFont(ofSize: 12, weight: .regular) public var moreBtnColor: UIColor = ThemeManager.shared.color.contentColor public var defaultBtnColor: UIColor = .white public var moreBtnBgColor: UIColor = .white public var defaultBtnBgColor: UIColor = ThemeManager.shared.color.mainColor public var moreBtnFont: UIFont = .systemFont(ofSize: 18, weight: .regular) public var defaultBtnFont: UIFont = .systemFont(ofSize: 18, weight: .medium) public init() {} } class DLAlertView: UIView { var defaultAction: (() -> Void)? var moreAction: (() -> Void)? lazy var contentView: UIView = { let view = UIView() view.backgroundColor = config.contentBgColor view.setCornerRadius(8) return view }() /// lazy var titleLabel: UILabel = { let label = UILabel() label.font = config.titleFont label.textColor = config.titleColor label.text = title label.numberOfLines = 0 label.textAlignment = .center return label }() lazy var subtitleLabel: UILabel = { let label = UILabel() label.font = config.subtitleFont label.textColor = config.subtitleColor label.attributedText = subtitleAttribute ?? NSAttributedString(string: subtitle) label.numberOfLines = 0 label.textAlignment = .center return label }() lazy var contentLabel: UILabel = { let label = UILabel() label.font = config.contentFont label.textColor = config.contentColor label.text = content // label.attributedText = subtitleAttribute ?? NSAttributedString(string: subtitle) label.numberOfLines = 0 label.textAlignment = .center return label }() /// lazy var moreBtn: UIButton = { let btn = UIButton() btn.titleLabel?.font = config.moreBtnFont btn.setTitle(moreBtnTitle, for: .normal) btn.setTitleColor(config.moreBtnColor, for: .normal) btn.setBackgroundColor(config.moreBtnBgColor, for: .normal) btn.borderColor = ThemeManager.shared.color.contentColor btn.borderWidth = 0.5 btn.addTapGestrue { [weak self] _ in self?.dismiss() self?.moreAction?() } btn.cornerRadius = 18 return btn }() lazy var defaultBtn: UIButton = { let btn = UIButton() btn.titleLabel?.font = config.defaultBtnFont btn.setTitle(defaultBtnTitle, for: .normal) btn.setTitleColor(config.defaultBtnColor, for: .normal) btn.setBackgroundColor(config.defaultBtnBgColor, for: .normal) btn.addTapGestrue { [weak self] _ in self?.dismiss() self?.defaultAction?() } btn.cornerRadius = 18 return btn }() /// line lazy var hLineView: UIView = { let view = UIView() view.backgroundColor = UIColor(hexStr: "#EEEEEE", alpha: 1.0) return view }() /// line lazy var vLineView: UIView = { let view = UIView() view.backgroundColor = UIColor(hexStr: "#EEEEEE", alpha: 1.0) return view }() /// btnView lazy var bottonView: UIStackView = { let view = UIStackView(arrangedSubviews: [moreBtn, defaultBtn]) view.axis = .horizontal view.spacing = 16 view.distribution = .fillEqually view.alignment = .center return view }() var config: DLAlertConfig var title: String = "" var subtitle: String = "" var subtitleAttribute: NSAttributedString? var content: String = "" var contentAttribute: NSAttributedString? var moreBtnTitle: String = "" var defaultBtnTitle: String = "" fileprivate weak var viewForPresent: UIView? init(title: String?=nil, subtitle: String?=nil, content: String?=nil, moreBtnTitle: String?=nil, defaultBtnTitle: String, config: DLAlertConfig = DLAlertConfig()) { self.config = config self.title = title ?? "" self.subtitle = subtitle ?? "" self.content = content ?? "" self.moreBtnTitle = moreBtnTitle ?? "" self.defaultBtnTitle = defaultBtnTitle super.init(frame: .zero) setupSubViews() } init(title: String?=nil, subtitleAttribute: NSAttributedString?=nil, contentAttribute: NSAttributedString?=nil, moreBtnTitle: String?=nil, defaultBtnTitle: String, config: DLAlertConfig = DLAlertConfig()) { self.config = config self.title = title ?? "" self.subtitleAttribute = subtitleAttribute self.contentAttribute = contentAttribute self.moreBtnTitle = moreBtnTitle ?? "" self.defaultBtnTitle = defaultBtnTitle super.init(frame: .zero) setupSubViews() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } func setupSubViews() { addSubview(contentView) contentView.layoutChain .centerX() .centerY(self, offset: -kScreenWidth * 0.1) .left(48, relation: .greaterThanOrEqual) .right(48, relation: .greaterThanOrEqual) .top(kNaviHeight, relation: .greaterThanOrEqual) .bottom(kNaviHeight, relation: .greaterThanOrEqual) .width(280, relation: .greaterThanOrEqual) var preView: UIView? if !title.isEmpty { // 标题 contentView.addSubview(titleLabel) titleLabel.layoutChain .top(18) .edgesHorzontal(20) preView = titleLabel } if !subtitle.isEmpty || subtitleAttribute != nil { // 子标题 contentView.addSubview(subtitleLabel) subtitleLabel.layoutChain .edgesHorzontal(20) if let preView = preView { subtitleLabel.layoutChain .topToBottomOfView(preView, offset: 10) } else { subtitleLabel.layoutChain .top(20) } preView = subtitleLabel } if !content.isEmpty { contentView.addSubview(contentLabel) contentLabel.layoutChain .edgesHorzontal(20) if let preView = preView { contentLabel.layoutChain .topToBottomOfView(preView, offset: 10) } else { contentLabel.layoutChain .top(20) } preView = contentLabel } // 底部按钮 moreBtn.isHidden = moreBtnTitle.isEmpty contentView.addSubview(bottonView) bottonView.layoutChain .edgesHorzontal(12) .height(50) .bottom(5) if let preView = preView { bottonView.layoutChain .topToBottomOfView(preView, offset: 20) } else { bottonView.layoutChain .top(20) } defaultBtn.layoutChain .height(36) moreBtn.layoutChain .height(36) // contentView.addSubview(hLineView) // hLineView.layoutChain // .edgesHorzontal() // .bottomToTopOfView(bottonView) // .height(0.5) // // if !moreBtnTitle.isEmpty { // contentView.addSubview(vLineView) // vLineView.layoutChain // .centerX() // .topToView(bottonView) // .bottomToView(bottonView) // .width(0.5) // } } } extension DLAlertView { func show(on view: UIView) { viewForPresent = view guard let viewForPresent = viewForPresent else { return } self.backgroundColor = .clear viewForPresent.addSubview(self) self.layoutChain .edges() alpha = 0 sizeToFit() contentView.center = .init(x: viewForPresent.frame.midX, y: viewForPresent.frame.midY) contentView.transform = transform.scaledBy(x: 0.8, y: 0.8) UIView.animate(withDuration: 0.25) { self.alpha = 1 self.contentView.transform = CGAffineTransform.identity self.backgroundColor = self.config.coverBgColor } completion: { _ in } } func dismiss() { UIView.animate(withDuration: 0.25) { self.alpha = 0 self.contentView.transform = self.contentView.transform.scaledBy(x: 0.2, y: 0.2) } completion: { _ in self.removeFromSuperview() } } }