Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
- }
- }
- }
Add Comment
Please, Sign In to add comment