Guest User

Untitled

a guest
Jun 25th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. import Foundation
  2. import UIKit
  3.  
  4. extension UIColor {
  5.  
  6. static let kAppColor = UIColor(red:0.0/255.0, green:151.0/255.0 ,blue:70.0/255.0, alpha:1.00)
  7. static let kDarkBlue = UIColor(red:0.0/255.0, green:116.0/255.0 ,blue:187.0/255.0, alpha:1.00)
  8. static let kRed = UIColor(red:242.0/255.0, green:37.0/255.0 ,blue:10.0/255.0, alpha:1.00)
  9. static let kDarkText = UIColor(red:38.0/255.0, green:38.0/255.0 ,blue:38.0/255.0, alpha:1.00)
  10. static let kLightBlue = UIColor(red:65.0/255.0, green:171.0/255.0 ,blue:248.0/255.0, alpha:1.00)
  11. static let kGray = UIColor(red:149.0/255.0, green:152.0/255.0 ,blue:154.0/255.0, alpha:1.00)
  12.  
  13. convenience init(red: Int, green: Int, blue: Int) {
  14. assert(red >= 0 && red <= 255, "Invalid red component")
  15. assert(green >= 0 && green <= 255, "Invalid green component")
  16. assert(blue >= 0 && blue <= 255, "Invalid blue component")
  17. self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
  18. }
  19.  
  20. convenience init(netHex:Int) {
  21. self.init(red:(netHex >> 16) & 0xff, green:(netHex >> 8) & 0xff, blue:netHex & 0xff)
  22. }
  23. }
  24.  
  25.  
  26. extension UIFont {
  27. static func regular(withSize size: CGFloat) -> UIFont {
  28. return UIFont(name: "Montserrat-Regular", size: size)!
  29. }
  30.  
  31. static func medium(withSize size: CGFloat) -> UIFont {
  32. return UIFont(name: "Montserrat-Medium", size: size)!
  33. }
  34.  
  35. static func bold(withSize size: CGFloat) -> UIFont {
  36. return UIFont(name: "Montserrat-Bold", size: size)!
  37. }
  38.  
  39. static func semiBold(withSize size: CGFloat) -> UIFont {
  40. return UIFont(name: "Montserrat-SemiBold", size: size)!
  41. }
  42.  
  43. static func light(withSize size: CGFloat) -> UIFont {
  44. return UIFont(name: "Montserrat-Light", size: size)!
  45. }
  46. }
  47.  
  48.  
  49.  
  50. extension UIViewController {
  51. func getStotyboardId() -> String { return String(describing: self) }
  52.  
  53. func getClassName() -> String {
  54. return NSStringFromClass(self.classForCoder).components(separatedBy:".").last!
  55. }
  56. }
  57.  
  58.  
  59. let imageCache = NSCache<AnyObject, AnyObject>()
  60.  
  61. extension UIImageView {
  62.  
  63. func downloadImageWithUrlString(urlString: String) -> Void {
  64.  
  65. if urlString.count == 0 { return }
  66.  
  67. self.image = nil
  68. if let cachedImage = imageCache.object(forKey: urlString as AnyObject) as? UIImage {
  69. self.image = cachedImage
  70. return
  71. }
  72.  
  73. let request = URLRequest(url: URL(string: urlString)!)
  74. let dataTask = URLSession.shared.dataTask(with: request) {data, response, error in
  75. if error != nil { return }
  76. DispatchQueue.main.async {
  77. let downloadedImage = UIImage(data: data!)
  78. if let image = downloadedImage {
  79. imageCache.setObject(image, forKey: urlString as AnyObject)
  80. self.image = UIImage(data: data!)
  81. }
  82. }
  83. }
  84. dataTask.resume()
  85. }
  86. }
  87.  
  88.  
  89. extension UILabel {
  90. var optimalHeight : CGFloat {
  91. get {
  92. let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.bounds.width, height: CGFloat.greatestFiniteMagnitude))
  93. label.numberOfLines = 0
  94. label.lineBreakMode = NSLineBreakMode.byWordWrapping
  95. label.font = self.font
  96. label.text = self.text
  97. label.sizeToFit()
  98. return label.frame.height
  99. }
  100. }
  101. }
  102.  
  103.  
  104. extension Date {
  105.  
  106. func daysBetween(date: Date) -> Int {
  107. return Date.daysBetween(start: self, end: date)
  108. }
  109.  
  110. static func daysBetween(start: Date, end: Date) -> Int {
  111. let calendar = Calendar.current
  112.  
  113. // Replace the hour (time) of both dates with 00:00
  114. let date1 = calendar.startOfDay(for: start)
  115. let date2 = calendar.startOfDay(for: end)
  116.  
  117. let a = calendar.dateComponents([.day], from: date1, to: date2)
  118. return a.value(for: .day)!
  119. }
  120.  
  121. static func dateFromNow(years: Int) -> Date {
  122. let currentCalendar = Calendar.current
  123. let now = Date()
  124. var components: DateComponents = DateComponents()
  125. components.year = years
  126. let dateFromNow: Date = currentCalendar.date(byAdding: components, to: now)!
  127. return dateFromNow
  128. }
  129. }
  130.  
  131.  
  132. extension UIButton {
  133. func makeRoundedShadow() {
  134. layer.cornerRadius = 5
  135. layer.masksToBounds = false
  136. layer.shadowOffset = CGSize(width: 5, height: 5)
  137. layer.shadowColor = UIColor.lightGray.cgColor
  138. layer.shadowRadius = 3
  139. layer.shadowOpacity = 0.7
  140. }
  141. }
Add Comment
Please, Sign In to add comment