Skip to content

Instantly share code, notes, and snippets.

@adamgraham
Last active May 18, 2019 03:13
Show Gist options
  • Save adamgraham/f27e0a8ce0eb26d8dfcea58f7026bb4b to your computer and use it in GitHub Desktop.
Save adamgraham/f27e0a8ce0eb26d8dfcea58f7026bb4b to your computer and use it in GitHub Desktop.
An @IBDesignable extension of the iOS class UIView to provide customization of borders within Interface Builder.
protocol DesignableBorder {
var cornerRadius: CGFloat { get set }
var borderWidth: CGFloat { get set }
var borderColor: UIColor? { get set }
}
@IBDesignable extension UIView: DesignableBorder {
@IBInspectable var cornerRadius: CGFloat {
get { return self.layer.cornerRadius }
set { self.layer.cornerRadius = newValue }
}
@IBInspectable var borderWidth: CGFloat {
get { return self.layer.borderWidth }
set { self.layer.borderWidth = newValue }
}
@IBInspectable var borderColor: UIColor? {
get {
guard let cgColor = self.layer.borderColor else { return nil }
return UIColor(cgColor: cgColor)
}
set { self.layer.borderColor = newValue?.cgColor }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment