ViewControllerExtensions.swift
7.86 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//
// ViewControllerExtensions.swift
// WarplySDKFrameworkIOS
//
// Created by Manos Chorianopoulos on 5/5/22.
//
import UIKit
extension UIViewController {
func setBackButton() {
let uiscreen: CGRect = UIScreen.main.bounds
let backButton = UIButton(type: UIButton.ButtonType.custom) as UIButton
backButton.frame = CGRect(x: 0, y: 0, width: uiscreen.height * 0.025, height: uiscreen.height * 0.02)
backButton.imageView!.contentMode = .scaleAspectFit
//backButton.setBackgroundImage(UIImage(named:Assets.Navigation.backButton), for: UIControlState())
backButton.setImage(UIImage(named: "ic_back", in: Bundle(for: MyEmptyClass.self), compatibleWith: nil), for: .normal)
// backButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: -20, bottom: 0, right: 0)
backButton.addTarget(self, action: #selector(moveToBack(_:)), for: .touchUpInside)
backButton.translatesAutoresizingMaskIntoConstraints = false
// Add width, height constraints
let widthContraints = NSLayoutConstraint(item: backButton, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: uiscreen.height * 0.025)
let heightContraints = NSLayoutConstraint(item: backButton, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: uiscreen.height * 0.02)
NSLayoutConstraint.activate([heightContraints,widthContraints])
let leftBarButtonItem: UIBarButtonItem = UIBarButtonItem(customView: backButton)
self.navigationItem.setLeftBarButton(leftBarButtonItem, animated: false)
self.navigationItem.title = ""
}
@objc func moveToBack(_ sender:UIButton){
self.navigationController?.popViewController(animated: true)
self.dismiss(animated: true, completion: {})
}
func setNavigationTitle(_ title: String, _ fontWeight: String? = "medium") {
let uiscreen: CGRect = UIScreen.main.bounds
let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: uiscreen.width * 0.7, height: uiscreen.height * 0.03))
titleLabel.text = title
titleLabel.textColor = UIColor(red: 0.21, green: 0.32, blue: 0.41, alpha: 1.00)
if (fontWeight == "bold") {
titleLabel.font = UIFont(name: "PFSquareSansPro-Bold", size: 17)
} else {
titleLabel.font = UIFont(name: "PFSquareSansPro-Medium", size: 17)
}
titleLabel.adjustsFontSizeToFitWidth = true
titleLabel.textAlignment = .center
self.navigationItem.titleView = titleLabel
}
}
extension String {
var htmlToAttributedString: NSAttributedString? {
guard let data = data(using: .utf8) else { return nil }
do {
return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
return nil
}
}
var htmlToString: String {
return htmlToAttributedString?.string ?? ""
}
}
extension String {
var length: Int {
return count
}
subscript (i: Int) -> String {
return self[i ..< i + 1]
}
func substring(fromIndex: Int) -> String {
return self[min(fromIndex, length) ..< length]
}
func substring(toIndex: Int) -> String {
return self[0 ..< max(0, toIndex)]
}
subscript (r: Range<Int>) -> String {
let range = Range(uncheckedBounds: (lower: max(0, min(length, r.lowerBound)),
upper: min(length, max(0, r.upperBound))))
let start = index(startIndex, offsetBy: range.lowerBound)
let end = index(start, offsetBy: range.upperBound - range.lowerBound)
return String(self[start ..< end])
}
}
extension UIView {
func addDashedBorderVertical(color: UIColor, width: CGFloat, height: CGFloat) {
let color = color.cgColor
let frameSize = self.frame.size
let lineLayer = CAShapeLayer()
lineLayer.strokeColor = color
lineLayer.lineWidth = width
lineLayer.lineDashPattern = [5,5]
let path = CGMutablePath()
path.addLines(between: [CGPoint(x: frameSize.width/2, y: 0), CGPoint(x: frameSize.width/2, y: height)])
lineLayer.path = path
self.layer.addSublayer(lineLayer)
}
}
extension UIImageView {
func load(link: String, placeholder: UIImage?, cache: URLCache? = nil) {
guard let url = URL(string: link) else { return }
let cache = cache ?? URLCache.shared
let request = URLRequest(url: url)
if let data = cache.cachedResponse(for: request)?.data, let image = UIImage(data: data) {
DispatchQueue.main.async {
self.image = image
}
} else {
self.image = placeholder
URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in
if let data = data, let response = response, ((response as? HTTPURLResponse)?.statusCode ?? 500) < 300, let image = UIImage(data: data) {
let cachedData = CachedURLResponse(response: response, data: data)
cache.storeCachedResponse(cachedData, for: request)
DispatchQueue.main.async {
self.image = image
}
}
}).resume()
}
}
}
typealias GradientPoints = (startPoint: CGPoint, endPoint: CGPoint)
enum GradientOrientation {
case topRightBottomLeft
case topLeftBottomRight
case horizontal
case vertical
var startPoint : CGPoint {
return points.startPoint
}
var endPoint : CGPoint {
return points.endPoint
}
var points : GradientPoints {
switch self {
case .topRightBottomLeft:
return (CGPoint(x: 0.0,y: 1.0), CGPoint(x: 1.0,y: 0.0))
case .topLeftBottomRight:
return (CGPoint(x: 0.0,y: 0.0), CGPoint(x: 1,y: 1))
case .horizontal:
return (CGPoint(x: 0.0,y: 0.5), CGPoint(x: 1.0,y: 0.5))
case .vertical:
return (CGPoint(x: 0.0,y: 0.0), CGPoint(x: 0.0,y: 1.0))
}
}
}
extension UIView {
func applyGradient(colours: [UIColor], locations: [NSNumber]? = nil, cornerRadius: CGFloat) {
let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = colours.map { $0.cgColor }
gradient.locations = locations
gradient.cornerRadius = cornerRadius
gradient.name = "linearGradientLayer"
self.layer.insertSublayer(gradient, at: 0)
}
func applyGradient(colours: [UIColor], gradient orientation: GradientOrientation, cornerRadius: CGFloat) {
let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = colours.map { $0.cgColor }
gradient.startPoint = orientation.startPoint
gradient.endPoint = orientation.endPoint
gradient.cornerRadius = cornerRadius
gradient.name = "linearGradientLayer"
self.layer.insertSublayer(gradient, at: 0)
}
}
extension UIViewController
{
func setupToHideKeyboardOnTapOnView()
{
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboardWarply))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboardWarply()
{
view.endEditing(true)
}
}
extension UIFont {
class func mediumSystemFont(ofSize pointSize: CGFloat) -> UIFont {
return self.systemFont(ofSize: pointSize, weight: .medium)
}
}