Last active
August 21, 2019 10:45
-
-
Save akhtarraza/5f9a37824f78d7164ad09bae27b9c4e3 to your computer and use it in GitHub Desktop.
UIView with border width, border color, corner radius
This file contains 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
import UIKit | |
protocol Decorating { | |
var borderWidth: CGFloat {get set} | |
var cornerRadius: CGFloat {get set} | |
var borderColor: UIColor {get set} | |
var shadowOffset: CGSize {get set} | |
} | |
extension Decorating where Self: UIView { | |
var cornerRadius: CGFloat { | |
get { | |
return self.layer.cornerRadius | |
} | |
set { | |
let rectShape = CAShapeLayer() | |
rectShape.bounds = self.frame | |
rectShape.position = self.center | |
rectShape.path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: [.bottomLeft , .bottomRight , .topLeft], cornerRadii: CGSize(width: 20, height: 20)).cgPath | |
self.layer.cornerRadius = newValue | |
self.layer.masksToBounds = true | |
} | |
} | |
var borderWidth: CGFloat { | |
get { | |
return self.layer.borderWidth | |
} | |
set { | |
self.layer.borderWidth = newValue | |
} | |
} | |
var borderColor: UIColor { | |
get { | |
return UIColor(cgColor: self.layer.borderColor!) | |
} | |
set { | |
self.layer.borderColor = newValue.cgColor | |
} | |
} | |
var shadowOffset: CGSize { | |
get { | |
return self.layer.shadowOffset | |
} | |
set { | |
self.layer.shadowOffset = newValue | |
} | |
} | |
var shadowColor: CGColor? { | |
get { | |
return self.layer.shadowColor | |
} | |
set { | |
layer.shadowColor = newValue | |
} | |
} | |
func addDropShadow(shadowColor: CGColor = UIColor.black.cgColor, | |
shadowOffset: CGSize = CGSize(width: 1.0, height: 2.0), | |
shadowOpacity: Float = 0.75, | |
shadowRadius: CGFloat = 3.0) { | |
layer.shadowColor = shadowColor | |
layer.shadowOffset = shadowOffset | |
layer.shadowOpacity = shadowOpacity | |
layer.shadowRadius = shadowRadius | |
} | |
} | |
extension UIView: Decorating { | |
@IBInspectable var shadow: Bool { | |
get { | |
return layer.shadowOpacity > 0.0 | |
} | |
set { | |
if newValue == true { | |
self.addDropShadow() | |
} | |
} | |
} | |
@IBInspectable var cornerRadius: CGFloat { | |
get { | |
return self.layer.cornerRadius | |
} | |
set { | |
self.layer.cornerRadius = newValue | |
// Don't touch the masksToBound property if a shadow is needed in addition to the cornerRadius | |
if shadow == false { | |
self.layer.masksToBounds = true | |
} | |
} | |
} | |
func roundCorners(corners:UIRectCorner, radius:CGFloat) { | |
if #available(iOS 11.0, *) { | |
clipsToBounds = true | |
layer.cornerRadius = radius | |
layer.maskedCorners = CACornerMask(rawValue: corners.rawValue) | |
} else { | |
let cornerPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) | |
let maskLayer = CAShapeLayer() | |
maskLayer.path = cornerPath.cgPath | |
layer.mask = maskLayer | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment