-
Notifications
You must be signed in to change notification settings - Fork 0
/
PKAlertController.swift
executable file
·185 lines (151 loc) · 8.11 KB
/
PKAlertController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
// PKAlertController.swift
//
// Created by Pramod Kumar on 16/01/19.
// Copyright © 2019 Pramod Kumar. All rights reserved.
//
import UIKit
open class PKAlertController {
//MARK:- Properties
//MARK:- Public
static let `default` = PKAlertController()
//MARK:- Private
private var topMostController: UIViewController? {
var presentedVC = UIApplication.shared.keyWindow?.rootViewController
while let pVC = presentedVC?.presentedViewController
{
presentedVC = pVC
}
if presentedVC == nil {
printDebug("PKAlertController Error: You don't have any views set. You may be calling in viewdidload. Try viewdidappear.")
}
return presentedVC
}
private var alertController: UIAlertController!
//MARK:- ViewLifeCycle
//MARK:-
private init() {}
//MARK:- Methods
//MARK:- Private
private func getAttributedString(string: String, font: UIFont?, color: UIColor?) -> NSMutableAttributedString {
let myMutableString = NSMutableAttributedString(string: string)
let range = NSRange(location: 0, length: string.count)
if let fnt = font {
myMutableString.addAttributes([NSAttributedString.Key.font : fnt], range: range)
}
if let clr = color {
myMutableString.addAttributes([NSAttributedString.Key.foregroundColor : clr], range: range)
}
return myMutableString
}
private func changeControllerTitle(title: String?, font: UIFont?, color: UIColor?) {
// Change Title With Color and Font:
guard let str = title else {return}
alertController.setValue(self.getAttributedString(string: str, font: font, color: color), forKey: "attributedTitle")
}
private func changeControllerMessage(message: String?, font: UIFont?, color: UIColor?) {
// Change Message With Color and Font:
guard let str = message else {return}
alertController.setValue(self.getAttributedString(string: str, font: font, color: color), forKey: "attributedMessage")
}
private func configureAlertAction(action: UIAlertAction, withData data: PKAlertButton) {
action.setValue(data.titleColor, forKey: "titleTextColor")
}
//MARK:- Public
func presentActionSheet(_ title: String?, titleFont: UIFont? = nil, titleColor: UIColor? = nil, message: String?, messageFont: UIFont? = AppFonts.SemiBold.withSize(14.0), messageColor: UIColor? = AppColors.themeGray40, sourceView: UIView, alertButtons: [PKAlertButton], cancelButton: PKAlertButton, tapBlock:((UIAlertAction,Int) -> Void)?) -> UIAlertController {
alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
self.changeControllerTitle(title: title, font: titleFont, color: titleColor)
self.changeControllerMessage(message: message, font: messageFont, color: messageColor)
//add all alert buttons
let closure: (UIAlertAction) -> Void = { (alert) in
printDebug(alert.title ?? "")
if let handel = tapBlock, let idx = alertButtons.firstIndex(where: { (button) -> Bool in
(alert.title ?? "") == button.title
}) {
handel(alert, idx)
}
}
for button in alertButtons {
let alertAction = UIAlertAction(title: button.title, style: .default, handler: closure)
self.configureAlertAction(action: alertAction, withData: button)
alertController.addAction(alertAction)
}
//add cancel button
let cancelAction = UIAlertAction(title: cancelButton.title, style: .cancel) { (alert) in
self.dismissActionSheet()
}
self.configureAlertAction(action: cancelAction, withData: cancelButton)
alertController.addAction(cancelAction)
alertController.popoverPresentationController?.sourceView = sourceView
alertController.popoverPresentationController?.sourceRect = sourceView.bounds
self.topMostController?.present(alertController, animated: true, completion: nil)
return alertController
}
//MARK:- Public
func presentActionSheetWithTextAllignMentAndImage(_ title: String?, message: String?, sourceView: UIView, alertButtons: [PKAlertButton], cancelButton: PKAlertButton, textAlignment: CATextLayerAlignmentMode = CATextLayerAlignmentMode.center , selectedButtonIndex: Int? = 0 , tapBlock:((UIAlertAction,Int) -> Void)?) -> UIAlertController {
alertController = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
//add all alert buttons
let closure: (UIAlertAction) -> Void = { (alert) in
printDebug(alert.title ?? "")
if let handel = tapBlock, let idx = alertButtons.firstIndex(where: { (button) -> Bool in
(alert.title ?? "") == button.title
}) {
handel(alert, idx)
}
}
for (index,button) in alertButtons.enumerated() {
let alertAction = UIAlertAction(title: button.title, style: .default, handler: closure)
alertAction.setValue(button.titleColor, forKey: "titleTextColor")
// alertAction.setValue(textAlignment, forKey: "titleTextAlignment")
if index == selectedButtonIndex {
alertAction.setValue(#imageLiteral(resourceName: "buttonCheckIcon"), forKey: "image")
} else {
alertAction.setValue(nil, forKey: "image")
}
alertController.addAction(alertAction)
}
//add cancel button
let cancelAction = UIAlertAction(title: cancelButton.title, style: .cancel) { (alert) in
self.dismissActionSheet()
}
cancelAction.setValue(cancelButton.titleColor, forKey: "titleTextColor")
alertController.addAction(cancelAction)
alertController.popoverPresentationController?.sourceView = sourceView
alertController.popoverPresentationController?.sourceRect = sourceView.bounds
self.topMostController?.present(alertController, animated: true, completion: nil)
return alertController
}
func presentActionSheetWithAttributed(_ title: NSMutableAttributedString?, message: NSMutableAttributedString?, sourceView: UIView, alertButtons: [PKAlertButton], cancelButton: PKAlertButton, tapBlock:((UIAlertAction,Int) -> Void)?) -> UIAlertController {
alertController = UIAlertController(title: "", message: "", preferredStyle: .actionSheet)
alertController.setValue(title, forKey: "attributedTitle")
alertController.setValue(message, forKey: "attributedMessage")
//add all alert buttons
let closure: (UIAlertAction) -> Void = { (alert) in
printDebug(alert.title ?? "")
if let handel = tapBlock, let idx = alertButtons.firstIndex(where: { (button) -> Bool in
(alert.title ?? "") == button.title
}) {
handel(alert, idx)
}
}
for button in alertButtons {
let alertAction = UIAlertAction(title: button.title, style: .default, handler: closure)
alertAction.setValue(button.titleColor, forKey: "titleTextColor")
alertController.addAction(alertAction)
}
//add cancel button
let cancelAction = UIAlertAction(title: cancelButton.title, style: .cancel) { (alert) in
self.dismissActionSheet()
}
cancelAction.setValue(cancelButton.titleColor, forKey: "titleTextColor")
alertController.addAction(cancelAction)
alertController.popoverPresentationController?.sourceView = sourceView
alertController.popoverPresentationController?.sourceRect = sourceView.bounds
self.topMostController?.present(alertController, animated: true, completion: nil)
return alertController
}
public func dismissActionSheet() {
self.alertController.dismiss(animated: true, completion: nil)
}
//MARK:- Action
}