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