OOAlertViewController.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // OOAlertViewController.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2017/11/24.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import UIKit
  9. public typealias OOAlertActionHandler = ((OOAlertAction) -> Void)
  10. /// Describes each action that is going to be shown in the 'AlertViewController'
  11. public class OOAlertAction {
  12. public let title: String
  13. public let style: OOAlertActionStyle
  14. public let handler: OOAlertActionHandler?
  15. /**
  16. Initialized an 'AlertAction'
  17. - parameter title: The title for the action, that will be used as the title for a button in the alert controller
  18. - parameter style: The style for the action, that will be used to style a button in the alert controller.
  19. - parameter handler: The handler for the action, that will be called when the user clicks on a button in the alert controller.
  20. - returns: An inmutable AlertAction object
  21. */
  22. public init(title: String, style: OOAlertActionStyle, handler: OOAlertActionHandler?) {
  23. self.title = title
  24. self.style = style
  25. self.handler = handler
  26. }
  27. }
  28. /**
  29. Describes the style for an action, that will be used to style a button in the alert controller.
  30. - Default: Green text label. Meant to draw attention to the action.
  31. - Cancel: Gray text label. Meant to be neutral.
  32. - Destructive: Red text label. Meant to warn the user about the action.
  33. */
  34. public enum OOAlertActionStyle {
  35. case `default`
  36. case cancel
  37. case destructive
  38. case custom(textColor: UIColor,backColor:UIColor)
  39. /**
  40. Decides which color to use for each style
  41. - returns: UIColor representing the color for the current style
  42. */
  43. func color() -> (UIColor,UIColor) {
  44. switch self {
  45. case .default:
  46. return (UIColor.white, O2ThemeManager.color(for: "Base.base_color")!)
  47. case .cancel:
  48. return (UIColor.white, UIColor(hex: "#333333"))
  49. case .destructive:
  50. return (O2ThemeManager.color(for: "Base.base_color")!, UIColor.white)
  51. case let .custom(textColor,backColor):
  52. return (textColor,backColor)
  53. }
  54. }
  55. }
  56. private enum Font: String {
  57. case Montserrat = "Montserrat-Regular"
  58. case SourceSansPro = "SourceSansPro-Regular"
  59. func font(_ size: CGFloat = 15.0) -> UIFont {
  60. return UIFont(name: self.rawValue, size: size)!
  61. }
  62. }
  63. public class OOAlertViewController: UIViewController {
  64. /// Text that will be used as the title for the alert
  65. public var titleText: String?
  66. /// Text that will be used as the body for the alert
  67. public var bodyText: String?
  68. /// If set to false, alert wont auto-dismiss the controller when an action is clicked. Dismissal will be up to the action's handler. Default is true.
  69. public var autoDismiss: Bool = true
  70. /// If autoDismiss is set to true, then set this property if you want the dismissal to be animated. Default is true.
  71. public var dismissAnimated: Bool = true
  72. fileprivate var actions = [OOAlertAction]()
  73. @IBOutlet weak var categoryIconImageView: UIImageView!
  74. @IBOutlet weak var titleLabel: UILabel!
  75. @IBOutlet weak var bodyLabel: UILabel!
  76. @IBOutlet weak var rightButton: UIButton!
  77. @IBOutlet weak var firstButton: UIButton!
  78. @IBOutlet weak var secondButton: UIButton!
  79. @IBOutlet weak var buttonContainerView: UIStackView!
  80. //替换默认View的方法
  81. override public func loadView() {
  82. let name = "OOAlertViewController"
  83. guard let view = Bundle.main.loadNibNamed(name, owner: self, options: nil)?.first as? UIView else {
  84. fatalError("Nib not found.")
  85. }
  86. view.layer.cornerRadius = 20
  87. view.layer.masksToBounds = true
  88. self.view = view
  89. }
  90. public override func viewDidLoad() {
  91. super.viewDidLoad()
  92. if actions.isEmpty {
  93. let okAction = OOAlertAction(title: "确定", style: .default, handler: nil)
  94. addAction(okAction)
  95. }
  96. setupLabels()
  97. setupButtons()
  98. }
  99. public override func didReceiveMemoryWarning() {
  100. super.didReceiveMemoryWarning()
  101. // Dispose of any resources that can be recreated.
  102. }
  103. private func setupButtons(){
  104. guard let firstAction = actions.first else { return }
  105. apply(firstAction, toButton: firstButton)
  106. if actions.count == 2 {
  107. let secondAction = actions.last!
  108. apply(secondAction, toButton: secondButton)
  109. } else {
  110. secondButton.removeFromSuperview()
  111. }
  112. }
  113. public func addAction(_ action: OOAlertAction) {
  114. guard actions.count < 2 else { return }
  115. actions += [action]
  116. }
  117. private func apply(_ action: OOAlertAction, toButton: UIButton) {
  118. let title = action.title
  119. let style = action.style
  120. toButton.setTitle(title, for: .normal)
  121. toButton.setTitleColor(style.color().0, for: .normal)
  122. }
  123. private func setupLabels() {
  124. titleLabel.text = titleText ?? "提示框"
  125. bodyLabel.text = bodyText ?? "这是一个提标框Demo"
  126. }
  127. @IBAction func didSelectFirstAction(_ sender: Any) {
  128. guard let firstAction = actions.first else { return }
  129. if let handler = firstAction.handler {
  130. handler(firstAction)
  131. }
  132. dismiss()
  133. }
  134. @IBAction func didSelectSecondAction(_ sender: Any) {
  135. guard let secondAction = actions.last, actions.count == 2 else { return }
  136. if let handler = secondAction.handler {
  137. handler(secondAction)
  138. }
  139. dismiss()
  140. }
  141. @IBAction func didCloseAction(_ sender: Any) {
  142. self.dismiss(animated: dismissAnimated, completion: nil)
  143. }
  144. // MARK: Helper's
  145. func dismiss() {
  146. guard autoDismiss else { return }
  147. self.dismiss(animated: dismissAnimated, completion: nil)
  148. }
  149. }