-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extensions.swift
61 lines (52 loc) · 2.21 KB
/
Extensions.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
extension UIColor {
struct CustomColors {
static let lead = UIColor.rgb(red: 30, green: 30, blue: 30)
static let customLightOrange = UIColor.rgb(red: 255, green: 138, blue: 101)
static let customOrange = UIColor.rgb(red: 255, green: 112, blue: 67)
static let customDarkOrange = UIColor.rgb(red: 244, green: 81, blue: 30)
static let whiteSmoke = UIColor.rgb(red: 245, green: 245, blue: 245)
static let ghostWhite = UIColor.rgb(red: 248, green: 248, blue: 255)
}
static func rgb(red: CGFloat, green: CGFloat, blue: CGFloat) -> UIColor {
return UIColor(red: red/255, green: green/255, blue: blue/255, alpha: 1)
}
}
extension UIView {
// Shortcut and modularized code for adding constraints
func addConstraintsWithFormat(format: String, views: UIView...) {
var viewsDict = [String: UIView]()
for (index, view) in views.enumerated() {
let key = "v\(index)"
view.translatesAutoresizingMaskIntoConstraints = false
viewsDict[key] = view
}
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDict))
}
}
extension UIImageView {
// Function to cache images
func loadImagesUsingCacheWithUrlString(urlString: String) {
let imageCache = NSCache<AnyObject, AnyObject>()
self.image = nil
// Check cache for image first
if let cachedImage = imageCache.object(forKey: urlString as AnyObject) as? UIImage{
self.image = cachedImage
return
}
// Otherwise fire off new download
let url = URL(string: urlString)
let request = URLRequest(url: url!)
URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in
if error != nil {
print(error!)
return
}
DispatchQueue.main.async {
if let downloadedImage = UIImage(data: data!) {
imageCache.setObject(downloadedImage, forKey: urlString as AnyObject)
self.image = downloadedImage
}
}
}).resume()
}
}