// // TSCustomAlertController.swift // Pods // // Created by 100Years on 2025/3/26. // import UIKit public class TSCustomAlertController { // MARK: - 配置模型 public struct AlertConfig { public var style: UIUserInterfaceStyle = .light public var message: String public var messageColor: UIColor = .white public var messageFont: UIFont = .systemFont(ofSize: 16) public var cancelTitle: String = "Retain".localized public var cancelColor: UIColor = .white public var confirmTitle: String = "Delete".localized public var confirmColor: UIColor = UIColor.themeColor public var cancelAction: (() -> Void)? public var confirmAction: (() -> Void)? public init(style:UIUserInterfaceStyle = .dark, message: String, messageColor: UIColor = .white, messageFont: UIFont = .systemFont(ofSize: 16), cancelTitle: String = "Retain".localized, cancelColor: UIColor = .white, confirmTitle: String = "Delete".localized, confirmColor: UIColor = UIColor.themeColor, cancelAction: ( () -> Void)? = nil, confirmAction: ( () -> Void)? = nil) { self.style = style self.message = message self.messageColor = messageColor self.messageFont = messageFont self.cancelTitle = cancelTitle self.cancelColor = cancelColor self.confirmTitle = confirmTitle self.confirmColor = confirmColor self.cancelAction = cancelAction self.confirmAction = confirmAction } } // MARK: - 显示弹窗 public static func show(in viewController: UIViewController, config: AlertConfig) { // 1. 创建AlertController (title设为空字符串而不是nil,避免自动上移) let alert = UIAlertController(title: "", message: "", preferredStyle: .alert) // 2. 自定义message let messageAttributed = NSAttributedString( string: config.message, attributes: [ .foregroundColor: config.messageColor, .font: config.messageFont ] ) alert.setValue(messageAttributed, forKey: "attributedMessage") // 设置黑暗模式 alert.overrideUserInterfaceStyle = config.style // 3. 添加取消按钮 let cancelAction = UIAlertAction(title: config.cancelTitle, style: .default) { _ in config.cancelAction?() } cancelAction.setValue(config.cancelColor, forKey: "titleTextColor") alert.addAction(cancelAction) // 4. 添加确定按钮 let confirmAction = UIAlertAction(title: config.confirmTitle, style: .default) { _ in config.confirmAction?() } confirmAction.setValue(config.confirmColor, forKey: "titleTextColor") alert.addAction(confirmAction) // 5. 显示弹窗 viewController.present(alert, animated: true) } }