Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Extensions.swift
- //
- // Created by Akash More on 23/09/17.
- // Copyright © 2017-2018 Akash More. All rights reserved.
- //
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this file , to deal in the Software without restriction, including without
- // limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
- // and/or sell copies of the File, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the File.
- //
- // THE FILE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE FILE OR THE USE OR OTHER DEALINGS IN
- // THE FILE.
- import UIKit
- import Lottie
- import CoreGraphics
- import AVFoundation
- extension UIView {
- func addConstraintsWithFormat(format: String, views: UIView...) {
- var viewsDictionary = [String: UIView]()
- for (index, view) in views.enumerated() {
- let key = "v\(index)"
- view.translatesAutoresizingMaskIntoConstraints = false
- viewsDictionary[key] = view
- }
- addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary))
- }
- }
- extension String{
- func getHeightOfStringText(TextString: String ,screenWidth: CGFloat) -> CGFloat {
- let sizee = CGSize(width: screenWidth - 10, height: 10000)
- let optionss = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
- let estimatedRect = NSString(string: TextString).boundingRect(with: sizee, options: optionss, attributes: [NSFontAttributeName: UIFont(name: "SFUIText-Medium", size: 15)], context: nil)
- //print("Descrip height++++++++\(estimatedRect.size.height)")
- return estimatedRect.size.height
- }
- }
- extension AVPlayer {
- var isPlaying: Bool {
- return rate != 0 && error == nil
- }
- }
- //below extension will change the height of a tabbar at the bottom to 45
- extension UITabBar {
- override open func sizeThatFits(_ size: CGSize) -> CGSize {
- super.sizeThatFits(size)
- var sizeThatFits = super.sizeThatFits(size)
- sizeThatFits.height = 45
- tintColor = UIColor.AppColor.Red.NavigationRed
- // tintColor = UIColor(red:0.90, green:0.57, blue:0.00, alpha:1.0)
- // backgroundColor = .red
- return sizeThatFits
- }
- }
- //this extension addes padding to the image
- extension UIImage {
- func addImagePadding(x: CGFloat, y: CGFloat) -> UIImage? {
- let width: CGFloat = self.size.width + x
- let height: CGFloat = self.size.width + y
- UIGraphicsBeginImageContextWithOptions(CGSize(width: width, height: height), false, 0)
- let origin: CGPoint = CGPoint(x: (width - self.size.width) / 2, y: (height - self.size.height) / 2)
- self.draw(at: origin)
- let imageWithPadding = UIGraphicsGetImageFromCurrentImageContext()
- UIGraphicsEndImageContext()
- return imageWithPadding
- }
- }
- //get color using hex value pound(#) sign is ommited... only char. should be entered(3fd946)
- extension UIColor {
- convenience init(hex: String) {
- let scanner = Scanner(string: hex)
- scanner.scanLocation = 0
- var rgbValue: UInt64 = 0
- scanner.scanHexInt64(&rgbValue)
- let r = (rgbValue & 0xff0000) >> 16
- let g = (rgbValue & 0xff00) >> 8
- let b = rgbValue & 0xff
- self.init(
- red: CGFloat(r) / 0xff,
- green: CGFloat(g) / 0xff,
- blue: CGFloat(b) / 0xff, alpha: 1
- )
- }
- }
- //This are the color used throughout the app
- extension UIColor {
- struct AppColor {
- struct Red {
- static let NavigationRed = UIColor(hex: "ed3851")
- }
- }
- }
- extension UIImage {
- class func colorForNavBar(color: UIColor) -> UIImage {
- let rect = CGRect(x: 0, y: 0, width: 1.0, height: 1.0)
- UIGraphicsBeginImageContext(rect.size)
- let context = UIGraphicsGetCurrentContext()
- context!.setFillColor(color.cgColor)
- context!.fill(rect)
- let image = UIGraphicsGetImageFromCurrentImageContext()
- UIGraphicsEndImageContext()
- return image!
- }
- }
- //used to create image with color
- //not used
- extension UIImage {
- func imageWithColor(tintColor: UIColor) -> UIImage {
- UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
- let context = UIGraphicsGetCurrentContext()! as CGContext
- context.translateBy(x: 0, y: self.size.height)
- context.scaleBy(x: 1.0, y: -1.0);
- context.setBlendMode(CGBlendMode.normal)
- let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
- context.clip(to: rect, mask: self.cgImage!)
- tintColor.setFill()
- context.fill(rect)
- let newImage = UIGraphicsGetImageFromCurrentImageContext()! as UIImage
- UIGraphicsEndImageContext()
- return newImage
- }
- }
- //scroll uiscrollview to bottom
- extension UIScrollView {
- func scrollToBottom() {
- let bottomOffset = CGPoint(x: 0, y: contentSize.height - bounds.size.height + contentInset.bottom)
- setContentOffset(bottomOffset, animated: true)
- }
- }
- extension UIScrollView {
- func scrollToTop() {
- let desiredOffset = CGPoint(x: 0, y: -contentInset.top)
- setContentOffset(desiredOffset, animated: true)
- }
- }
- //show progress activity indicator inside UIButton
- extension UIButton {
- func loadingIndicator(show: Bool) {
- if show {
- let indicator = UIActivityIndicatorView()
- // indicator.color = UIColor.AppColor.Red.NavigationRed
- let buttonHeight = self.bounds.size.height
- let buttonWidth = self.bounds.size.width
- indicator.center = CGPoint(x: buttonWidth-30, y: buttonHeight/2)
- self.addSubview(indicator)
- indicator.startAnimating()
- } else {
- for view in self.subviews {
- if let indicator = view as? UIActivityIndicatorView {
- indicator.stopAnimating()
- indicator.removeFromSuperview()
- }
- }
- }
- }
- }
- /*notification extension specifies notification name that is going to be used in two different vc.
- this will help change notification name for both vc at the same time and eliminates any chance of typing errors
- */
- extension NSNotification.Name {
- static let saveDateOnly = NSNotification.Name(rawValue: "saveDateOnly")
- static let verifyEmail = NSNotification.Name(rawValue: "verifyEmail")
- static let infoUpdated = NSNotification.Name(rawValue: "infoUpdated")
- }
- extension UICollectionView {
- func showEmptyMessage(_ message: String) {
- // let messageLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.bounds.size.width, height: self.bounds.size.height))
- // messageLabel.text = message
- // messageLabel.textColor = .black
- // messageLabel.numberOfLines = 0;
- // messageLabel.textAlignment = .center;
- // messageLabel.font = UIFont(name: "TrebuchetMS", size: 15)
- // messageLabel.sizeToFit()
- //
- // self.backgroundView = messageLabel;
- let animationView = LOTAnimationView(name: "floating_cloud")
- // let animationView = LOTAnimationView(name: "empty_list")
- // animationView.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
- // animationView.center = (self.backgroundView?.center)!
- animationView.contentMode = .scaleAspectFit
- animationView.backgroundColor = UIColor.white
- // view.addSubview(animationView)
- self.backgroundView = animationView
- self.backgroundView?.backgroundColor = UIColor(hex: "f1f1f1")
- animationView.loopAnimation = true
- animationView.play()
- }
- func removeEmptyMessage() {
- self.backgroundView = nil
- }
- }
- //==================== Refresh Controller start
- extension HomeControllerMain {
- func addRefreshController() {
- let refreshControl = UIRefreshControl()
- refreshControl.backgroundColor = UIColor.red
- refreshControl.tintColor = UIColor.yellow
- self.collectionView?.addSubview(refreshControl)
- }
- }
- //==================== Refresh Controller end
Add Comment
Please, Sign In to add comment