Created
March 15, 2019 10:14
-
-
Save greglecki/31f2e607394a27725e4c014649e29710 to your computer and use it in GitHub Desktop.
Useful extensions for UIView like rounded corners, shadows, border, etc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // RoundedCornerView.swift | |
| // RoundedCornerShadowViewTest | |
| // | |
| // Created by Greg Lecki on 15/03/2019. | |
| // Copyright © 2019 Greg Lecki. All rights reserved. | |
| // | |
| import UIKit | |
| // MARK: - Properties | |
| @IBDesignable | |
| extension UIView { | |
| /// SwifterSwift: Border color of view; also inspectable from Storyboard. | |
| @IBInspectable public var borderColor: UIColor? { | |
| get { | |
| guard let color = layer.borderColor else { return nil } | |
| return UIColor(cgColor: color) | |
| } | |
| set { | |
| guard let color = newValue else { | |
| layer.borderColor = nil | |
| return | |
| } | |
| // Fix React-Native conflict issue | |
| guard String(describing: type(of: color)) != "__NSCFType" else { return } | |
| layer.borderColor = color.cgColor | |
| } | |
| } | |
| /// SwifterSwift: Border width of view; also inspectable from Storyboard. | |
| @IBInspectable public var borderWidth: CGFloat { | |
| get { | |
| return layer.borderWidth | |
| } | |
| set { | |
| layer.borderWidth = newValue | |
| } | |
| } | |
| /// SwifterSwift: Corner radius of view; also inspectable from Storyboard. | |
| @IBInspectable public var cornerRadius: CGFloat { | |
| get { | |
| return layer.cornerRadius | |
| } | |
| set { | |
| //layer.masksToBounds = true | |
| layer.cornerRadius = abs(CGFloat(Int(newValue * 100)) / 100) | |
| } | |
| } | |
| /// SwifterSwift: Shadow color of view; also inspectable from Storyboard. | |
| @IBInspectable public var shadowColor: UIColor? { | |
| get { | |
| guard let color = layer.shadowColor else { return nil } | |
| return UIColor(cgColor: color) | |
| } | |
| set { | |
| layer.shadowColor = newValue?.cgColor | |
| } | |
| } | |
| /// SwifterSwift: Shadow offset of view; also inspectable from Storyboard. | |
| @IBInspectable public var shadowOffset: CGSize { | |
| get { | |
| return layer.shadowOffset | |
| } | |
| set { | |
| layer.shadowOffset = newValue | |
| } | |
| } | |
| /// SwifterSwift: Shadow opacity of view; also inspectable from Storyboard. | |
| @IBInspectable public var shadowOpacity: Float { | |
| get { | |
| return layer.shadowOpacity | |
| } | |
| set { | |
| layer.shadowOpacity = newValue | |
| } | |
| } | |
| /// SwifterSwift: Shadow radius of view; also inspectable from Storyboard. | |
| @IBInspectable public var shadowRadius: CGFloat { | |
| get { | |
| return layer.shadowRadius | |
| } | |
| set { | |
| layer.shadowRadius = newValue | |
| } | |
| } | |
| } | |
| // MARK: - Methods | |
| public extension UIView { | |
| /// SwifterSwift: Set some or all corners radiuses of view. | |
| /// | |
| /// - Parameters: | |
| /// - corners: array of corners to change (example: [.bottomLeft, .topRight]). | |
| /// - radius: radius for selected corners. | |
| public func roundCorners(_ corners: UIRectCorner, radius: CGFloat) { | |
| let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) | |
| let shape = CAShapeLayer() | |
| shape.path = maskPath.cgPath | |
| layer.mask = shape | |
| } | |
| /// SwifterSwift: Add shadow to view. | |
| /// | |
| /// - Parameters: | |
| /// - color: shadow color (default is #137992). | |
| /// - radius: shadow radius (default is 3). | |
| /// - offset: shadow offset (default is .zero). | |
| /// - opacity: shadow opacity (default is 0.5). | |
| public func addShadow(ofColor color: UIColor = UIColor(red: 0.07, green: 0.47, blue: 0.57, alpha: 1.0), radius: CGFloat = 3, offset: CGSize = .zero, opacity: Float = 0.5) { | |
| layer.shadowColor = color.cgColor | |
| layer.shadowOffset = offset | |
| layer.shadowRadius = radius | |
| layer.shadowOpacity = opacity | |
| layer.masksToBounds = false | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment