Guest User

Untitled

a guest
Jan 20th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. import UIKit
  2. import Foundation
  3.  
  4. public class AlertService {
  5.  
  6. private init() {}
  7. public static let shared = AlertService()
  8.  
  9. //////////////////////////////////////////////////////////////////
  10.  
  11. func showAlert(controller: UIViewController,
  12. title: String?,
  13. message: String?,
  14. titleColor: UIColor = UIColor.black,
  15. messageColor: UIColor = UIColor.black,
  16. backgroundColor: UIColor = UIColor.white){
  17.  
  18. let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
  19. alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
  20.  
  21. if let title = title{
  22. if titleColor != UIColor.black{
  23. let titleString = NSAttributedString(string: title, attributes: [
  24. NSAttributedStringKey.font : UIFont.systemFont(ofSize: 20),
  25. NSAttributedStringKey.foregroundColor : titleColor
  26. ])
  27. alert.setValue(titleString, forKey: "attributedTitle")
  28. }
  29. }
  30.  
  31. if let message = message{
  32. if messageColor != UIColor.black{
  33. let messageString = NSAttributedString(string: message, attributes: [
  34. NSAttributedStringKey.font : UIFont.systemFont(ofSize: 16),
  35. NSAttributedStringKey.foregroundColor : messageColor
  36. ])
  37. alert.setValue(messageString, forKey: "attributedMessage")
  38. }
  39. }
  40.  
  41. if backgroundColor != UIColor.white{
  42. let subview = (alert.view.subviews.first?.subviews.first?.subviews.first!)! as UIView
  43. subview.backgroundColor = backgroundColor
  44. }
  45.  
  46. controller.present(alert, animated: true, completion: nil)
  47. }
  48.  
  49.  
  50. //////////////////////////////////////////////////////////////////
  51.  
  52. func showAlertForConfirmation (controller: UIViewController,
  53. title: String?,
  54. message: String,
  55. negativeBtnText: String?,
  56. positiveBtnText: String,
  57. onCompletion: @escaping (_ isPositiveBtnCliked: Bool)-> Void){
  58.  
  59. let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
  60.  
  61. let negativeBtn = UIAlertAction(title: negativeBtnText, style: .cancel){ (_) in
  62. onCompletion(false)
  63. }
  64. let positiveBtn = UIAlertAction(title: positiveBtnText, style: .default) { (_) in
  65. onCompletion(true)
  66. }
  67.  
  68. alert.addAction(negativeBtn)
  69. alert.addAction(positiveBtn)
  70.  
  71. controller.present(alert, animated: true, completion: nil)
  72. }
  73.  
  74.  
  75. ///////////////////////////////////////////////////////////////////
  76. func showMessage(controller: UIViewController,
  77. message: String,
  78. messageColor: UIColor = UIColor.black,
  79. backgroundColor:UIColor = UIColor.white){
  80.  
  81. let alert = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet)
  82.  
  83. if backgroundColor != UIColor.white{
  84. let subview = (alert.view.subviews.first?.subviews.first?.subviews.first!)! as UIView
  85. subview.backgroundColor = backgroundColor
  86. }
  87.  
  88. if messageColor != UIColor.black{
  89. let attributedMessage = NSAttributedString(string: message, attributes: [
  90. NSAttributedStringKey.foregroundColor: messageColor
  91. ])
  92. alert.setValue(attributedMessage, forKey: "attributedMessage")
  93. }
  94.  
  95. controller.present(alert, animated: true, completion: nil)
  96. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.5){
  97. alert.dismiss(animated: true, completion: nil)
  98. }
  99. }
  100. }
Add Comment
Please, Sign In to add comment