RBToast
平平无奇的tost弹窗,一句代码展示toast弹窗。支持黑色,白色,以及自定义风格显示,支持自定义显示时长
使用
RBToast.showMessage("提示信息")
RBToast.showMessage("白色风格信息", style: .light, time: 6)
RBToast.showMessage("自定义风格信息", style: .custom(textColor: .red, textFont: .systemFont(ofSize: 20, weight: .medium), backgroundColor: .yellow), time: 3)
代码
import UIKit
enum RBToastaStyle {
case dark
case light
case custom(textColor: UIColor, textFont: UIFont, backgroundColor: UIColor)
}
class RBToast: UIView {
let defauleDarkTextColor: UIColor = .white
let defauleDarkTextFont: UIFont = .systemFont(ofSize: 14)
let defauleDarkBackgroundColor: UIColor = UIColor(red: 0.275, green: 0.275, blue: 0.275, alpha: 1)
let defauleLightTextColor: UIColor = UIColor(red: 0.233, green: 0.199, blue: 0.199, alpha: 1)
let defauleLightTextFont: UIFont = .systemFont(ofSize: 14)
let defauleLightBackgroundColor: UIColor = .white
private var textLabel: UILabel = {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width - 60, height: 10000))
label.numberOfLines = 0
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
loadSubViews()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func loadSubViews() {
addSubview(textLabel)
}
private func refreshUI(text: String, style: RBToastaStyle) {
self.textLabel.text = text
switch style {
case .dark:
self.textLabel.font = defauleDarkTextFont
self.textLabel.textColor = defauleDarkTextColor
self.backgroundColor = defauleDarkBackgroundColor
case .light:
self.textLabel.font = defauleLightTextFont
self.textLabel.textColor = defauleLightTextColor
self.backgroundColor = defauleLightBackgroundColor
case .custom(let textColor, let textFont, let backgroundColor):
self.textLabel.font = textFont
self.textLabel.textColor = textColor
self.backgroundColor = backgroundColor
}
let edg = UIEdgeInsets(top: 12, left: 22, bottom: 12, right: 22)
let SW = UIScreen.main.bounds.size.width
let SH = UIScreen.main.bounds.size.height
self.textLabel.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width - 80 - edg.left - edg.right, height: CGFloat(MAXFLOAT))
self.textLabel.sizeToFit()
let H: CGFloat = min(self.textLabel.bounds.height, SH - 160)
let W = self.textLabel.bounds.width
self.frame = CGRect(x: (SW - W - edg.left - edg.right)/2, y: (SH - H - edg.top - edg.bottom)/2, width: W + edg.left + edg.right, height: H + edg.top + edg.bottom)
self.textLabel.frame = CGRect(x: edg.left, y: edg.top, width: W, height: H)
self.layer.cornerRadius = 8
self.layer.shadowColor = UIColor(white: 0.88, alpha: 1).cgColor
self.layer.shadowOpacity = 0.4
self.layer.shadowRadius = 8
self.layer.shadowOffset = .zero
}
class func showMessage(_ message: String, style: RBToastaStyle = .dark, time: TimeInterval = 3) {
DispatchQueue.main.async {
let toastView = RBToast(frame: .zero)
toastView.refreshUI(text: message, style: style)
if let window = getWindow() {
let X = (window.bounds.width - toastView.bounds.width)/2
let Y = (window.bounds.height - toastView.bounds.height)/2
toastView.frame = CGRect(x: X, y: Y, width: toastView.bounds.width, height: toastView.bounds.height)
toastView.alpha = 0
window.addSubview(toastView)
UIView.animate(withDuration: 0.2, delay: 0) {
toastView.alpha = 1
}
DispatchQueue.main.asyncAfter(deadline: .now() + time) {
UIView.animate(withDuration: 0.2) {
toastView.alpha = 0
} completion: { _ in
toastView.removeFromSuperview()
}
}
}
}
}
class func getWindow() -> UIWindow? {
let app = UIApplication.shared
if #available(iOS 13.0, *) {
if let window = app.connectedScenes
.filter({$0.activationState == .foregroundActive})
.map({$0 as? UIWindowScene})
.compactMap({$0})
.first?.windows
.filter({$0.isKeyWindow}).first{
return window
}else if let window = UIApplication.shared.delegate?.window{
return window
}else{
return nil
}
} else {
if let window = UIApplication.shared.delegate?.window{
return window
}else{
return nil
}
}
}
}
效果


