Skip to content

Instantly share code, notes, and snippets.

@yukin01
Last active February 23, 2021 05:11
Show Gist options
  • Save yukin01/122b82a5ad6499cd9c28747672068ba1 to your computer and use it in GitHub Desktop.
Save yukin01/122b82a5ad6499cd9c28747672068ba1 to your computer and use it in GitHub Desktop.
Add @IBDesignable and @IBInspectable to UIView, UIButton, UIImageView and UITextField
import UIKit
protocol Roundable {
var cornerRadius: CGFloat { get set }
}
protocol Borderable {
var borderWidth: CGFloat { get set }
var borderColor: UIColor? { get set }
}
protocol Shadowable {
var shadowRadius: CGFloat { get set }
var shadowOpacity: Float { get set }
var shadowOffset: CGSize { get set }
var shadowColor: UIColor? { get set }
}
@available(*, unavailable, message: "Only use it at Storybord or Xib.", renamed: "UIView")
@IBDesignable final class DesignableView: UIView, Roundable, Borderable, Shadowable {
@IBInspectable var cornerRadius: CGFloat {
get { return layer.cornerRadius }
set { layer.cornerRadius = newValue }
}
@IBInspectable var borderWidth: CGFloat {
get { return layer.borderWidth }
set { layer.borderWidth = newValue }
}
@IBInspectable var borderColor: UIColor? {
get { return layer.borderColor.map { UIColor(cgColor: $0) } }
set { layer.borderColor = newValue?.cgColor }
}
@IBInspectable var shadowRadius: CGFloat {
get { return layer.shadowRadius }
set { layer.shadowRadius = newValue }
}
@IBInspectable var shadowOpacity: Float {
get { return layer.shadowOpacity }
set { layer.shadowOpacity = newValue }
}
@IBInspectable var shadowOffset: CGSize {
get { return layer.shadowOffset }
set { layer.shadowOffset = newValue }
}
@IBInspectable var shadowColor: UIColor? {
get { return layer.shadowColor.map { UIColor(cgColor: $0) } }
set { layer.shadowColor = newValue?.cgColor }
}
}
@available(*, unavailable, message: "Only use it at Storybord or Xib.", renamed: "UIButton")
@IBDesignable final class DesignableButton: UIButton, Roundable, Borderable, Shadowable {
@IBInspectable var cornerRadius: CGFloat {
get { return layer.cornerRadius }
set { layer.cornerRadius = newValue }
}
@IBInspectable var borderWidth: CGFloat {
get { return layer.borderWidth }
set { layer.borderWidth = newValue }
}
@IBInspectable var borderColor: UIColor? {
get { return layer.borderColor.map { UIColor(cgColor: $0) } }
set { layer.borderColor = newValue?.cgColor }
}
@IBInspectable var shadowRadius: CGFloat {
get { return layer.shadowRadius }
set { layer.shadowRadius = newValue }
}
@IBInspectable var shadowOpacity: Float {
get { return layer.shadowOpacity }
set { layer.shadowOpacity = newValue }
}
@IBInspectable var shadowOffset: CGSize {
get { return layer.shadowOffset }
set { layer.shadowOffset = newValue }
}
@IBInspectable var shadowColor: UIColor? {
get { return layer.shadowColor.map { UIColor(cgColor: $0) } }
set { layer.shadowColor = newValue?.cgColor }
}
}
@available(*, unavailable, message: "Only use it at Storybord or Xib.", renamed: "UIImageView")
@IBDesignable final class DesignableImageView: UIImageView, Roundable, Borderable {
@IBInspectable var cornerRadius: CGFloat {
get { return layer.cornerRadius }
set { layer.cornerRadius = newValue }
}
@IBInspectable var borderWidth: CGFloat {
get { return layer.borderWidth }
set { layer.borderWidth = newValue }
}
@IBInspectable var borderColor: UIColor? {
get { return layer.borderColor.map { UIColor(cgColor: $0) } }
set { layer.borderColor = newValue?.cgColor }
}
}
@available(*, unavailable, message: "Only use it at Storybord or Xib.", renamed: "UIView")
@IBDesignable final class DesignableRoundView: UIView, Borderable, Shadowable {
override func layoutSubviews() {
super.layoutSubviews()
layer.cornerRadius = frame.width / 2
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
layer.cornerRadius = frame.width / 2
}
@IBInspectable var borderWidth: CGFloat {
get { return layer.borderWidth }
set { layer.borderWidth = newValue }
}
@IBInspectable var borderColor: UIColor? {
get { return layer.borderColor.map { UIColor(cgColor: $0) } }
set { layer.borderColor = newValue?.cgColor }
}
@IBInspectable var shadowRadius: CGFloat {
get { return layer.shadowRadius }
set { layer.shadowRadius = newValue }
}
@IBInspectable var shadowOpacity: Float {
get { return layer.shadowOpacity }
set { layer.shadowOpacity = newValue }
}
@IBInspectable var shadowOffset: CGSize {
get { return layer.shadowOffset }
set { layer.shadowOffset = newValue }
}
@IBInspectable var shadowColor: UIColor? {
get { return layer.shadowColor.map { UIColor(cgColor: $0) } }
set { layer.shadowColor = newValue?.cgColor }
}
}
@available(*, unavailable, message: "Only use it at Storybord or Xib.", renamed: "UIButton")
@IBDesignable final class DesignableRoundButton: UIButton, Borderable, Shadowable {
override func layoutSubviews() {
super.layoutSubviews()
layer.cornerRadius = frame.width / 2
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
layer.cornerRadius = frame.width / 2
}
@IBInspectable var borderWidth: CGFloat {
get { return layer.borderWidth }
set { layer.borderWidth = newValue }
}
@IBInspectable var borderColor: UIColor? {
get { return layer.borderColor.map { UIColor(cgColor: $0) } }
set { layer.borderColor = newValue?.cgColor }
}
@IBInspectable var shadowRadius: CGFloat {
get { return layer.shadowRadius }
set { layer.shadowRadius = newValue }
}
@IBInspectable var shadowOpacity: Float {
get { return layer.shadowOpacity }
set { layer.shadowOpacity = newValue }
}
@IBInspectable var shadowOffset: CGSize {
get { return layer.shadowOffset }
set { layer.shadowOffset = newValue }
}
@IBInspectable var shadowColor: UIColor? {
get { return layer.shadowColor.map { UIColor(cgColor: $0) } }
set { layer.shadowColor = newValue?.cgColor }
}
}
@available(*, unavailable, message: "Only use it at Storybord or Xib.", renamed: "UIImageView")
@IBDesignable final class DesignableRoundImageView: UIImageView, Borderable {
override func layoutSubviews() {
super.layoutSubviews()
layer.cornerRadius = frame.width / 2
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
layer.cornerRadius = frame.width / 2
}
@IBInspectable var borderWidth: CGFloat {
get { return layer.borderWidth }
set { layer.borderWidth = newValue }
}
@IBInspectable var borderColor: UIColor? {
get { return layer.borderColor.map { UIColor(cgColor: $0) } }
set { layer.borderColor = newValue?.cgColor }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment