Cool Extension Swift 4.2


1. Corner Radius for UIView

import UIKit

extension UIView {
    
    @IBInspectable
    var cornerRadius: CGFloat {
        get {
            return layer.cornerRadius
        }
        set {
            layer.cornerRadius = newValue
            layer.masksToBounds = newValue > 0
        }
    }
}
How to Use :




2. Extention UIColor


import UIKit

extension UIColor {
    convenience init(r: CGFloat, g: CGFloat, b: CGFloat) {
        self.init(red: r / 255, green: g / 255, blue: b / 255, alpha: 1)
    }
}
How to Use :

override func viewDidLoad() {
        super.viewDidLoad()
        
        UIColor(r: 20, g: 150, b: 50)
}

3. UItextField max Length

import UIKit
private var __maxLengths = [UITextField: Int]()
extension UITextField {
    @IBInspectable var maxLength: Int {
        get {
            guard let l = __maxLengths[self] else {
                return 3 // (global default-limit. or just, Int.max)
            }
            return l
        }
        set {
            __maxLengths[self] = newValue
            addTarget(self, action: #selector(fix), for: .editingChanged)
        }
    }

    @objc func fix(textField: UITextField) {
        let t = textField.text
        textField.text = t?.safelyLimitedTo(length: maxLength)
    }
}

extension String {
    func safelyLimitedTo(length n: Int) -> String {
        if count <= n {
            return self
        }
        return String(Array(self).prefix(upTo: n))
    }
}

How to Use :



4. Extension roundCorner for UIview or UIButton

you can set corner radius: Up, bottom, left & right.

//UIView

extension UIView {
    func roundCornersView(corners: UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
    }
}
}   
//UIButton   
extension UIButton { func roundCornersButton(corners: UIRectCorner, radius: CGFloat) { let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) let mask = CAShapeLayer() mask.path = path.cgPath self.layer.mask = mask } }
How to Use :

//UIView

self.yourView.roundCornersView(corners: [.bottomRight, .bottomLeft], radius: 10)
//UIView

self.yourButton.roundCornersButton(corners: .bottomRight, radius: 10)


5. Extension UIView -> CircleView

import UIKit

class CircleView: UIView {
    
    override func draw(_ rect: CGRect) {
        layer.cornerRadius = frame.size.width / 2
   
    } 

}
How to Use :

in UIView.xib 



6. Extension UIView -> Border UIview class


import UIKit

class CustomViewBorder: UIView {
    override func draw(_ rect: CGRect) {
        layer.masksToBounds = true
        layer.borderColor = #colorLiteral(red: 0.4588235294, green: 0.7176470588, blue: 0.1803921569, alpha: 1)
        layer.borderWidth = 1
    }
}
How to Use :
just use like before.



7. Extension Array -> Remove duplicate items from an array

extension Array where Element: Hashable {
    func removingDuplicates() -> [Element] {
        var addedDict = [Element: Bool]()
        
        return filter {
            addedDict.updateValue(true, forKey: $0) == nil
        }
    }
    
    mutating func removeDuplicates() {
        self = self.removingDuplicates()
    }
}


How to Use :

just use like before.

var dataArray = ["a","a","a","b","b","c","c","e","f","c"]
dataArray.removeDuplicates()

print(dataTab) // result = ["a", "b", "c", "e", "f"]



8. Set unsafe area background color 



How to Use :

//use extention UIApplication above
UIApplication.shared.statusBarView?.backgroundColor = .blue


9.  Extention UIView ->  Shadow  UIview

extension UIView {
    func dropShadow(scale: Bool = true) {
        layer.borderWidth = 1
        layer.borderColor = UIColor.clear.cgColor
        layer.masksToBounds = false
        
        layer.shadowColor = UIColor.lightGray.cgColor
        layer.shadowOffset = CGSize(width: -1, height: 1)
        layer.shadowRadius = 4
        layer.shadowOpacity = 0.9
    }
}
How to Use :


class viewController: UIViewController {

@IBOutlet weak var viewView: UIView!

override public func viewDidLoad() {
        super.viewDidLoad()
        
        self.viewView.dropShadow()
        
 }
}


Update 05-10-2018

10.  Extention String -> Split String

extension String {
    func group(of n: Int) -> [String] {
        let chars = Array(self)
        return stride(from: 0, to: chars.count, by: n).map {
            String(chars[$0..<min($0+n, chars.count)])
        }
    }
}

How to Use :

 override func viewDidLoad() {
        super.viewDidLoad()

     let dataString = "TestData"
     let groupNo = dataString.group(of: 4) // "4" For range you split
}
















No comments: