// // DLAlertPopVC.swift // dinoGo // // Created by osell on 2022/12/8. // Copyright © 2022 dino. All rights reserved. // import UIKit import URLNavigator public class DLAlertPopVC: DLCustomPopVC { // MARK: - Accessor public var confirmBlock: (() -> Void)? public var cancelBlock: (() -> Void)? public var titleText: String? { didSet { titleLabel.text = titleText } } public var contentText: String? { didSet { contentLabel.text = contentText } } public var cancelText: String? { didSet { cancelBtn.setTitle(cancelText, for: .normal) } } public var confirmText: String? { didSet { confirmBtn.setTitle(confirmText, for: .normal) } } // MARK: - Subviews /// title lazy var titleLabel: UILabel = { let label = UILabel() label.font = UIFont.systemFont(ofSize: 15, weight: .semibold) label.textColor = UIColor(hexStr: "#0E0E0E") return label }() /// close lazy var closeBtn: UIButton = { let btn = UIButton(type: .custom) btn.setImage(UIImage(named: "Common/close"), for: .normal) btn.contentEdgeInsets = UIEdgeInsets(top: 7.5, left: 7.5, bottom: 7.5, right: 7.5) btn.addTouchBlock { [weak self] _ in self?.dismiss(animated: true) } btn.isHidden = true return btn }() /// text lazy var contentLabel: UILabel = { let label = UILabel() label.font = UIFont.systemFont(ofSize: 13, weight: .medium) label.textColor = UIColor(hexStr: "#0E0E0E") label.numberOfLines = 0 return label }() /// lazy var stackView: UIStackView = { let view = UIStackView(arrangedSubviews: [cancelBtn, confirmBtn]) view.spacing = 10 view.axis = .horizontal view.alignment = .fill view.distribution = .fillEqually return view }() /// 确认 lazy var cancelBtn: UIButton = { let btn = UIButton() btn.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: .heavy) btn.setTitle("oapp_Cancel".localizedString, for: .normal) btn.setTitleColor(UIColor(hexStr: "#0E0E0E"), for: .normal) btn.layer.borderWidth = 1 btn.layer.borderColor = UIColor(hexStr: "#0E0E0E").cgColor btn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5) btn.setBackgroundColor(UIColor(hexStr: "#F5F5F5"), for: .normal) btn.addTouchBlock { [weak self] _ in self?.dismiss(animated: true, completion: { self?.cancelBlock?() }) } return btn }() /// 确认 lazy var confirmBtn: UIButton = { let btn = UIButton() btn.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: .heavy) btn.setTitle("oapp_Confirm".localizedString, for: .normal) btn.setTitleColor(.white, for: .normal) btn.setBackgroundColor(ThemeManager.shared.color.mainColor, for: .normal) btn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5) btn.addTouchBlock { [weak self] _ in self?.dismiss(animated: true, completion: { self?.confirmBlock?() }) } return btn }() // MARK: - Lifecycle override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) popStyle = .center centerContentInset = 10 centerCornerRadius = 0 } public required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } public override func viewDidLoad() { super.viewDidLoad() setupSubviews() setupLayout() } } // MARK: - Setup private extension DLAlertPopVC { private func setupSubviews() { contentView.addSubview(closeBtn) if titleText != nil, titleText?.isEmpty == false { contentView.addSubview(titleLabel) } if contentText != nil, contentText?.isEmpty == false { contentView.addSubview(contentLabel) } if cancelText?.isEmpty == false || confirmText?.isEmpty == false { contentView.addSubview(stackView) } cancelBtn.isHidden = !(cancelText?.isEmpty == false) confirmBtn.isHidden = !(confirmText?.isEmpty == false) } private func setupLayout() { closeBtn.layoutChain .top(5) .right(5) .size(CGSize(width: 30, height: 30)) var tempView: UIView? if titleText != nil, titleText?.isEmpty == false { titleLabel.layoutChain .left(15) .top(15) .rightToLeftOfView(closeBtn, offset: -10) tempView = titleLabel } if contentText != nil, contentText?.isEmpty == false { if let tempView = tempView { contentLabel.layoutChain .left(15) .right(15) .topToBottomOfView(tempView, offset: 25) } else { contentLabel.layoutChain .top(15) .left(15) .rightToLeftOfView(closeBtn, offset: -10) } tempView = contentLabel } guard let tempView = tempView else { return } if cancelText?.isEmpty == false || confirmText?.isEmpty == false { stackView.layoutChain .topToBottomOfView(tempView, offset: 30) .left(15) .right(15) .bottom(15) .height(44) } } } // MARK: - Public extension DLAlertPopVC { } // MARK: - Request private extension DLAlertPopVC { } // MARK: - Action @objc private extension DLAlertPopVC { } // MARK: - Private private extension DLAlertPopVC { } public extension UIViewController { /// 显示弹窗 /// - Parameters: /// - title: 标题 /// - message: 文本内容 /// - confirmText: 确认按钮文案 /// - confirmBlock: 确认按钮点击回调 /// - cancelText: 取消按钮文案 /// - cancelBlock: 取消按钮点击回调 func showAlert(showCloseBtn: Bool = true, title: String?=nil, message: String?=nil, confirmText: String?=nil, confirmBlock: (() -> Void)?=nil, cancelText: String?=nil, cancelBlock: (() -> Void)?=nil) { if title == nil && message == nil { return } let vc = DLAlertPopVC() vc.titleText = title vc.contentText = message vc.confirmText = confirmText vc.confirmBlock = confirmBlock vc.cancelText = cancelText vc.cancelBlock = cancelBlock vc.closeBtn.isHidden = !showCloseBtn vc.dimmingClick = showCloseBtn present(vc, animated: true) } static func showAlert(showCloseBtn: Bool = true, title: String?=nil, message: String?=nil, confirmText: String?=nil, confirmBlock: (() -> Void)?=nil, cancelText: String?=nil, cancelBlock: (() -> Void)?=nil) { if title == nil && message == nil { return } let vc = DLAlertPopVC() vc.titleText = title vc.contentText = message vc.confirmText = confirmText vc.confirmBlock = confirmBlock vc.cancelText = cancelText vc.cancelBlock = cancelBlock vc.closeBtn.isHidden = !showCloseBtn vc.dimmingClick = showCloseBtn UIViewController.topMost?.present(vc, animated: true) } }