-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathExtensions.swift
More file actions
252 lines (197 loc) · 8.22 KB
/
Copy pathExtensions.swift
File metadata and controls
252 lines (197 loc) · 8.22 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//
// Extensions.swift
// NMG.CRM
//
// Created by Soyombo bat-erdene on 4/30/18.
// Copyright © 2018 soyombo bat-erdene. All rights reserved.
//
import UIKit
import Apollo
extension UIViewController {
func navigate(_ navigation: MyNavigation) {
navigate(navigation as Navigation)
}
}
extension String {
public var withoutHtml: String {
guard let data = self.data(using: .utf8) else {
return self
}
let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
]
guard let attributedString = try? NSAttributedString(data: data, options: options, documentAttributes: nil) else {
return self
}
var str = attributedString.string
str = str.replacingOccurrences(of: "\n", with: "")
return str
}
}
extension Date {
var millisecondsSince1970:Int {
return Int((self.timeIntervalSince1970 * 1000.0).rounded())
}
init(milliseconds:Int64) {
self = Date(timeIntervalSince1970: TimeInterval(milliseconds / 1000))
}
}
extension UIView {
func addBlurEffect()
{
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = self.bounds
blurEffectView.tag = 1000
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] // for supporting device rotation
self.addSubview(blurEffectView)
}
func heightForView(text:String, font:UIFont, width:CGFloat) -> CGFloat{
let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.font = font
label.text = text
label.sizeToFit()
return label.frame.height
}
func dropShadow(color: UIColor, opacity: Float = 0.5, offSet: CGSize, radius: CGFloat = 1, scale: Bool = true) {
self.layer.masksToBounds = false
self.layer.shadowColor = color.cgColor
self.layer.shadowOpacity = opacity
self.layer.shadowOffset = offSet
self.layer.shadowRadius = radius
self.layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
self.layer.shouldRasterize = true
self.layer.rasterizationScale = scale ? UIScreen.main.scale : 1
}
func addBorder(with color:UIColor){
self.layer.borderWidth = 1
self.layer.borderColor = color.cgColor
}
}
extension UIImageView {
func setImageWithString(text: String, backGroundColor: UIColor, attributes: [NSAttributedStringKey : Any]) {
var displayString: NSMutableString = NSMutableString(string: "")
var words = text.components(separatedBy: CharacterSet.whitespacesAndNewlines)
if words.count == 2 {
let firstWord = words[0]
if firstWord.count != 0 {
let firstLetter = firstWord.substring(to: firstWord.index(firstWord.startIndex, offsetBy: 1))
displayString.append(firstLetter)
}
let secondWord = words[1]
if secondWord.count != 0 {
let firstLetter = secondWord.substring(to: (secondWord.index((secondWord.startIndex), offsetBy: 1)))
displayString.append(".")
displayString.append(firstLetter)
}
}else{
let firstWord = words[0]
if firstWord.count != 0 {
let firstLetter = firstWord.substring(to: firstWord.index(firstWord.startIndex, offsetBy: 1))
displayString.append(firstLetter)
}
}
let scale:Float = Float(UIScreen.main.scale)
var size:CGSize = self.bounds.size
if (self.contentMode == .scaleToFill ||
self.contentMode == .scaleAspectFill ||
self.contentMode == .scaleAspectFit ||
self.contentMode == .redraw)
{
size.width = CGFloat(floorf(Float(size.width) * scale) / scale)
size.height = CGFloat(floorf(Float(size.height) * scale) / scale)
}
UIGraphicsBeginImageContextWithOptions(size, false, CGFloat( scale))
let context = UIGraphicsGetCurrentContext()
let path = CGPath(ellipseIn: self.bounds, transform: nil)
context!.addPath(path)
context?.clip()
context!.setFillColor(backGroundColor.cgColor)
context!.fill(CGRect(x:0, y:0, width:size.width, height:size.height));
let textSize = displayString.size(withAttributes: attributes)
displayString.draw(in: CGRect(x: bounds.size.width/2 - textSize.width/2, y: bounds.size.height/2 - textSize.height/2, width: textSize.width, height: textSize.height), withAttributes: attributes)
let image = UIGraphicsGetImageFromCurrentImageContext()
self.image = image
}
func fontName(fontName:String) -> UIFont {
let fontSize:CGFloat = self.bounds.width * 40
if fontName.count != 0 {
return UIFont(name: fontName, size: fontSize)!
}else{
return UIFont.systemFont(ofSize: fontSize)
}
}
}
public extension UIImage {
public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {
let rect = CGRect(origin: .zero, size: size)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
color.setFill()
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
guard let cgImage = image?.cgImage else { return nil }
self.init(cgImage: cgImage)
}
func tint(with color: UIColor) -> UIImage
{
UIGraphicsBeginImageContextWithOptions(self.size, false, UIScreen.main.scale)
guard let context = UIGraphicsGetCurrentContext() else { return self }
// flip the image
context.scaleBy(x: 1.0, y: -1.0)
context.translateBy(x: 0.0, y: -self.size.height)
// multiply blend mode
context.setBlendMode(.multiply)
let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
context.clip(to: rect, mask: self.cgImage!)
color.setFill()
context.fill(rect)
// create UIImage
guard let newImage = UIGraphicsGetImageFromCurrentImageContext() else { return self }
UIGraphicsEndImageContext()
return newImage
}
}
public typealias JSON = [String: JSONDecodable & JSONEncodable]
extension Dictionary: JSONDecodable {
public init(jsonValue value: JSONValue) throws {
guard let dictionary = forceBridgeFromObjectiveC(value) as? Dictionary else {
throw JSONDecodingError.couldNotConvert(value: value, to: Dictionary.self)
}
self = dictionary
}
}
extension Array: JSONDecodable {
public init(jsonValue value: JSONValue) throws {
guard let array = forceBridgeFromObjectiveC(value) as? Array else {
throw JSONDecodingError.couldNotConvert(value: value, to: Array.self)
}
self = array
}
}
private func forceBridgeFromObjectiveC(_ value: Any) -> Any {
switch value {
case is NSString:
return value as! String
case is Bool:
return value as! Bool
case is Int:
return value as! Int
case is Double:
return value as! Double
case is NSDictionary:
return Dictionary(uniqueKeysWithValues: (value as! NSDictionary).map { ( $0.key as! AnyHashable, forceBridgeFromObjectiveC($0.value)) })
case is NSArray:
return (value as! NSArray).map { forceBridgeFromObjectiveC($0) }
default:
return value
}
}
extension Array {
func contain<T>(obj: T) -> Bool where T : Equatable {
return self.filter({$0 as? T == obj}).count > 0
}
}