Skip to content

Instantly share code, notes, and snippets.

@jylock
Created July 22, 2016 02:23
Show Gist options
  • Select an option

  • Save jylock/1b9c46125791ae5829975454478ecacc to your computer and use it in GitHub Desktop.

Select an option

Save jylock/1b9c46125791ae5829975454478ecacc to your computer and use it in GitHub Desktop.
IBInspectable Setup
import UIKit
extension UIView
{
@IBInspectable var cornerRadius: CGFloat {
get {
return self.layer.cornerRadius
}
set {
self.layer.cornerRadius = newValue
self.layer.masksToBounds = newValue > 0
}
}
@IBInspectable var borderColor: UIColor? {
get {
return UIColor(CGColor: self.layer.borderColor!)
}
set {
self.layer.borderColor = newValue?.CGColor
}
}
@IBInspectable var borderWidth: CGFloat {
get {
return self.layer.borderWidth
}
set {
self.layer.borderWidth = newValue
}
}
@IBInspectable var leftBorder: CGFloat {
get {
return 0.0
}
set {
let line = UIView(frame: CGRect(x: 0.0, y: 0.0, width: newValue, height: self.bounds.height))
line.translatesAutoresizingMaskIntoConstraints = false
line.backgroundColor = self.borderColor
self.addSubview(line)
let views = ["line": line]
let metrics = ["lineWidth": newValue]
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[line(==lineWidth)]", options: [], metrics: metrics, views: views))
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[line]|", options: [], metrics: nil, views: views))
}
}
@IBInspectable var topBorder: CGFloat {
get {
return 0.0
}
set {
let line = UIView(frame: CGRect(x: 0.0, y: 0.0, width: self.bounds.width, height: newValue))
line.translatesAutoresizingMaskIntoConstraints = false
line.backgroundColor = self.borderColor
self.addSubview(line)
let views = ["line": line]
let metrics = ["lineWidth": newValue]
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[line]|", options: [], metrics: nil, views: views))
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[line(==lineWidth)]", options: [], metrics: metrics, views: views))
}
}
@IBInspectable var rightBorder: CGFloat {
get {
return 0.0
}
set {
let line = UIView(frame: CGRect(x: self.bounds.width, y: 0.0, width: newValue, height: self.bounds.height))
line.translatesAutoresizingMaskIntoConstraints = false
line.backgroundColor = self.borderColor
self.addSubview(line)
let views = ["line": line]
let metrics = ["lineWidth": newValue]
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("[line(==lineWidth)]|", options: [], metrics: metrics, views: views))
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[line]|", options: [], metrics: nil, views: views))
}
}
@IBInspectable var bottomBorder: CGFloat {
get {
return 0.0
}
set {
let line = UIView(frame: CGRect(x: 0.0, y: self.bounds.height, width: self.bounds.width, height: newValue))
line.translatesAutoresizingMaskIntoConstraints = false
line.backgroundColor = self.borderColor
self.addSubview(line)
let views = ["line": line]
let metrics = ["lineWidth": newValue]
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[line]|", options: [], metrics: nil, views: views))
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[line(==lineWidth)]|", options: [], metrics: metrics, views: views))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment