Created
January 20, 2019 09:53
-
-
Save shishirthedev/927a172088c849e5e3dd22a892e38ea1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| import Foundation | |
| public class AlertService { | |
| private init() {} | |
| public static let shared = AlertService() | |
| ////////////////////////////////////////////////////////////////// | |
| func showAlert(controller: UIViewController, | |
| title: String?, | |
| message: String?, | |
| titleColor: UIColor = UIColor.black, | |
| messageColor: UIColor = UIColor.black, | |
| backgroundColor: UIColor = UIColor.white){ | |
| let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) | |
| alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) | |
| if let title = title{ | |
| if titleColor != UIColor.black{ | |
| let titleString = NSAttributedString(string: title, attributes: [ | |
| NSAttributedStringKey.font : UIFont.systemFont(ofSize: 20), | |
| NSAttributedStringKey.foregroundColor : titleColor | |
| ]) | |
| alert.setValue(titleString, forKey: "attributedTitle") | |
| } | |
| } | |
| if let message = message{ | |
| if messageColor != UIColor.black{ | |
| let messageString = NSAttributedString(string: message, attributes: [ | |
| NSAttributedStringKey.font : UIFont.systemFont(ofSize: 16), | |
| NSAttributedStringKey.foregroundColor : messageColor | |
| ]) | |
| alert.setValue(messageString, forKey: "attributedMessage") | |
| } | |
| } | |
| if backgroundColor != UIColor.white{ | |
| let subview = (alert.view.subviews.first?.subviews.first?.subviews.first!)! as UIView | |
| subview.backgroundColor = backgroundColor | |
| } | |
| controller.present(alert, animated: true, completion: nil) | |
| } | |
| ////////////////////////////////////////////////////////////////// | |
| func showAlertForConfirmation (controller: UIViewController, | |
| title: String?, | |
| message: String, | |
| negativeBtnText: String?, | |
| positiveBtnText: String, | |
| onCompletion: @escaping (_ isPositiveBtnCliked: Bool)-> Void){ | |
| let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) | |
| let negativeBtn = UIAlertAction(title: negativeBtnText, style: .cancel){ (_) in | |
| onCompletion(false) | |
| } | |
| let positiveBtn = UIAlertAction(title: positiveBtnText, style: .default) { (_) in | |
| onCompletion(true) | |
| } | |
| alert.addAction(negativeBtn) | |
| alert.addAction(positiveBtn) | |
| controller.present(alert, animated: true, completion: nil) | |
| } | |
| /////////////////////////////////////////////////////////////////// | |
| func showMessage(controller: UIViewController, | |
| message: String, | |
| messageColor: UIColor = UIColor.black, | |
| backgroundColor:UIColor = UIColor.white){ | |
| let alert = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet) | |
| if backgroundColor != UIColor.white{ | |
| let subview = (alert.view.subviews.first?.subviews.first?.subviews.first!)! as UIView | |
| subview.backgroundColor = backgroundColor | |
| } | |
| if messageColor != UIColor.black{ | |
| let attributedMessage = NSAttributedString(string: message, attributes: [ | |
| NSAttributedStringKey.foregroundColor: messageColor | |
| ]) | |
| alert.setValue(attributedMessage, forKey: "attributedMessage") | |
| } | |
| controller.present(alert, animated: true, completion: nil) | |
| DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.5){ | |
| alert.dismiss(animated: true, completion: nil) | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment