Guest User

Untitled

a guest
Mar 24th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.84 KB | None | 0 0
  1. //
  2. // Extensions.swift
  3. //
  4. // Created by Akash More on 23/09/17.
  5. // Copyright © 2017-2018 Akash More. All rights reserved.
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this file , to deal in the Software without restriction, including without
  10. // limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. // and/or sell copies of the File, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the File.
  16. //
  17. // THE FILE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE FILE OR THE USE OR OTHER DEALINGS IN
  23. // THE FILE.
  24.  
  25. import UIKit
  26. import Lottie
  27. import CoreGraphics
  28. import AVFoundation
  29.  
  30. extension UIView {
  31. func addConstraintsWithFormat(format: String, views: UIView...) {
  32. var viewsDictionary = [String: UIView]()
  33. for (index, view) in views.enumerated() {
  34. let key = "v\(index)"
  35. view.translatesAutoresizingMaskIntoConstraints = false
  36. viewsDictionary[key] = view
  37. }
  38.  
  39. addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary))
  40. }
  41. }
  42.  
  43. extension String{
  44. func getHeightOfStringText(TextString: String ,screenWidth: CGFloat) -> CGFloat {
  45.  
  46. let sizee = CGSize(width: screenWidth - 10, height: 10000)
  47. let optionss = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
  48. let estimatedRect = NSString(string: TextString).boundingRect(with: sizee, options: optionss, attributes: [NSFontAttributeName: UIFont(name: "SFUIText-Medium", size: 15)], context: nil)
  49. //print("Descrip height++++++++\(estimatedRect.size.height)")
  50. return estimatedRect.size.height
  51. }
  52. }
  53.  
  54. extension AVPlayer {
  55. var isPlaying: Bool {
  56. return rate != 0 && error == nil
  57. }
  58. }
  59.  
  60. //below extension will change the height of a tabbar at the bottom to 45
  61. extension UITabBar {
  62. override open func sizeThatFits(_ size: CGSize) -> CGSize {
  63. super.sizeThatFits(size)
  64. var sizeThatFits = super.sizeThatFits(size)
  65. sizeThatFits.height = 45
  66. tintColor = UIColor.AppColor.Red.NavigationRed
  67. // tintColor = UIColor(red:0.90, green:0.57, blue:0.00, alpha:1.0)
  68. // backgroundColor = .red
  69. return sizeThatFits
  70. }
  71. }
  72.  
  73.  
  74.  
  75. //this extension addes padding to the image
  76. extension UIImage {
  77.  
  78. func addImagePadding(x: CGFloat, y: CGFloat) -> UIImage? {
  79. let width: CGFloat = self.size.width + x
  80. let height: CGFloat = self.size.width + y
  81. UIGraphicsBeginImageContextWithOptions(CGSize(width: width, height: height), false, 0)
  82. let origin: CGPoint = CGPoint(x: (width - self.size.width) / 2, y: (height - self.size.height) / 2)
  83. self.draw(at: origin)
  84. let imageWithPadding = UIGraphicsGetImageFromCurrentImageContext()
  85. UIGraphicsEndImageContext()
  86.  
  87. return imageWithPadding
  88. }
  89. }
  90.  
  91. //get color using hex value pound(#) sign is ommited... only char. should be entered(3fd946)
  92. extension UIColor {
  93. convenience init(hex: String) {
  94. let scanner = Scanner(string: hex)
  95. scanner.scanLocation = 0
  96.  
  97. var rgbValue: UInt64 = 0
  98.  
  99. scanner.scanHexInt64(&rgbValue)
  100.  
  101. let r = (rgbValue & 0xff0000) >> 16
  102. let g = (rgbValue & 0xff00) >> 8
  103. let b = rgbValue & 0xff
  104.  
  105. self.init(
  106. red: CGFloat(r) / 0xff,
  107. green: CGFloat(g) / 0xff,
  108. blue: CGFloat(b) / 0xff, alpha: 1
  109. )
  110. }
  111. }
  112.  
  113. //This are the color used throughout the app
  114. extension UIColor {
  115. struct AppColor {
  116. struct Red {
  117. static let NavigationRed = UIColor(hex: "ed3851")
  118. }
  119.  
  120. }
  121. }
  122.  
  123.  
  124. extension UIImage {
  125. class func colorForNavBar(color: UIColor) -> UIImage {
  126. let rect = CGRect(x: 0, y: 0, width: 1.0, height: 1.0)
  127. UIGraphicsBeginImageContext(rect.size)
  128. let context = UIGraphicsGetCurrentContext()
  129.  
  130. context!.setFillColor(color.cgColor)
  131. context!.fill(rect)
  132.  
  133. let image = UIGraphicsGetImageFromCurrentImageContext()
  134. UIGraphicsEndImageContext()
  135.  
  136.  
  137. return image!
  138. }
  139. }
  140.  
  141. //used to create image with color
  142. //not used
  143. extension UIImage {
  144. func imageWithColor(tintColor: UIColor) -> UIImage {
  145. UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
  146.  
  147. let context = UIGraphicsGetCurrentContext()! as CGContext
  148. context.translateBy(x: 0, y: self.size.height)
  149. context.scaleBy(x: 1.0, y: -1.0);
  150. context.setBlendMode(CGBlendMode.normal)
  151.  
  152. let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
  153. context.clip(to: rect, mask: self.cgImage!)
  154. tintColor.setFill()
  155. context.fill(rect)
  156.  
  157. let newImage = UIGraphicsGetImageFromCurrentImageContext()! as UIImage
  158. UIGraphicsEndImageContext()
  159.  
  160. return newImage
  161. }
  162. }
  163.  
  164. //scroll uiscrollview to bottom
  165. extension UIScrollView {
  166.  
  167. func scrollToBottom() {
  168. let bottomOffset = CGPoint(x: 0, y: contentSize.height - bounds.size.height + contentInset.bottom)
  169. setContentOffset(bottomOffset, animated: true)
  170. }
  171. }
  172. extension UIScrollView {
  173. func scrollToTop() {
  174. let desiredOffset = CGPoint(x: 0, y: -contentInset.top)
  175. setContentOffset(desiredOffset, animated: true)
  176. }
  177. }
  178.  
  179. //show progress activity indicator inside UIButton
  180. extension UIButton {
  181. func loadingIndicator(show: Bool) {
  182. if show {
  183. let indicator = UIActivityIndicatorView()
  184. // indicator.color = UIColor.AppColor.Red.NavigationRed
  185. let buttonHeight = self.bounds.size.height
  186. let buttonWidth = self.bounds.size.width
  187. indicator.center = CGPoint(x: buttonWidth-30, y: buttonHeight/2)
  188. self.addSubview(indicator)
  189. indicator.startAnimating()
  190. } else {
  191. for view in self.subviews {
  192. if let indicator = view as? UIActivityIndicatorView {
  193. indicator.stopAnimating()
  194. indicator.removeFromSuperview()
  195. }
  196. }
  197. }
  198. }
  199. }
  200.  
  201. /*notification extension specifies notification name that is going to be used in two different vc.
  202. this will help change notification name for both vc at the same time and eliminates any chance of typing errors
  203. */
  204.  
  205. extension NSNotification.Name {
  206. static let saveDateOnly = NSNotification.Name(rawValue: "saveDateOnly")
  207. static let verifyEmail = NSNotification.Name(rawValue: "verifyEmail")
  208. static let infoUpdated = NSNotification.Name(rawValue: "infoUpdated")
  209. }
  210.  
  211. extension UICollectionView {
  212.  
  213. func showEmptyMessage(_ message: String) {
  214. // let messageLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.bounds.size.width, height: self.bounds.size.height))
  215. // messageLabel.text = message
  216. // messageLabel.textColor = .black
  217. // messageLabel.numberOfLines = 0;
  218. // messageLabel.textAlignment = .center;
  219. // messageLabel.font = UIFont(name: "TrebuchetMS", size: 15)
  220. // messageLabel.sizeToFit()
  221. //
  222. // self.backgroundView = messageLabel;
  223.  
  224.  
  225.  
  226.  
  227. let animationView = LOTAnimationView(name: "floating_cloud")
  228. // let animationView = LOTAnimationView(name: "empty_list")
  229. // animationView.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
  230. // animationView.center = (self.backgroundView?.center)!
  231. animationView.contentMode = .scaleAspectFit
  232. animationView.backgroundColor = UIColor.white
  233. // view.addSubview(animationView)
  234. self.backgroundView = animationView
  235. self.backgroundView?.backgroundColor = UIColor(hex: "f1f1f1")
  236. animationView.loopAnimation = true
  237. animationView.play()
  238. }
  239.  
  240. func removeEmptyMessage() {
  241. self.backgroundView = nil
  242. }
  243. }
  244.  
  245.  
  246. //==================== Refresh Controller start
  247.  
  248. extension HomeControllerMain {
  249.  
  250. func addRefreshController() {
  251.  
  252. let refreshControl = UIRefreshControl()
  253. refreshControl.backgroundColor = UIColor.red
  254. refreshControl.tintColor = UIColor.yellow
  255. self.collectionView?.addSubview(refreshControl)
  256. }
  257. }
  258. //==================== Refresh Controller end
Add Comment
Please, Sign In to add comment