Created
December 9, 2015 05:24
-
-
Save jwthanh/4b1c7945d84e71e79a48 to your computer and use it in GitHub Desktop.
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 | |
// MARK: - UIView | |
extension UIView { | |
@IBInspectable var cornerRadius: CGFloat { | |
get { | |
return layer.cornerRadius | |
} | |
set { | |
layer.cornerRadius = newValue | |
layer.masksToBounds = newValue > 0 | |
} | |
} | |
@IBInspectable var borderWidth: CGFloat { | |
get { | |
return layer.borderWidth | |
} | |
set { | |
layer.borderWidth = newValue | |
} | |
} | |
@IBInspectable var borderColor: UIColor? { | |
get { | |
return UIColor(CGColor: layer.borderColor!) | |
} | |
set { | |
layer.borderColor = newValue?.CGColor | |
} | |
} | |
@IBInspectable var leftBorderWidth: CGFloat { | |
get { | |
return 0.0 // Just to satisfy property | |
} | |
set { | |
let line = UIView(frame: CGRect(x: 0.0, y: 0.0, width: newValue, height: bounds.height)) | |
line.translatesAutoresizingMaskIntoConstraints = false | |
line.backgroundColor = UIColor(CGColor: layer.borderColor!) | |
self.addSubview(line) | |
let views = ["line": line] | |
let metrics = ["lineWidth": newValue] | |
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[line(==lineWidth)]", options: [], metrics: metrics, views: views)) | |
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[line]|", options: [], metrics: nil, views: views)) | |
} | |
} | |
@IBInspectable var topBorderWidth: CGFloat { | |
get { | |
return 0.0 // Just to satisfy property | |
} | |
set { | |
let line = UIView(frame: CGRect(x: 0.0, y: 0.0, width: bounds.width, height: newValue)) | |
line.translatesAutoresizingMaskIntoConstraints = false | |
line.backgroundColor = borderColor | |
self.addSubview(line) | |
let views = ["line": line] | |
let metrics = ["lineWidth": newValue] | |
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[line]|", options: [], metrics: nil, views: views)) | |
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[line(==lineWidth)]", options: [], metrics: metrics, views: views)) | |
} | |
} | |
@IBInspectable var rightBorderWidth: CGFloat { | |
get { | |
return 0.0 // Just to satisfy property | |
} | |
set { | |
let line = UIView(frame: CGRect(x: bounds.width, y: 0.0, width: newValue, height: bounds.height)) | |
line.translatesAutoresizingMaskIntoConstraints = false | |
line.backgroundColor = borderColor | |
self.addSubview(line) | |
let views = ["line": line] | |
let metrics = ["lineWidth": newValue] | |
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("[line(==lineWidth)]|", options: [], metrics: metrics, views: views)) | |
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[line]|", options: [], metrics: nil, views: views)) | |
} | |
} | |
@IBInspectable var bottomBorderWidth: CGFloat { | |
get { | |
return 0.0 // Just to satisfy property | |
} | |
set { | |
let line = UIView(frame: CGRect(x: 0.0, y: bounds.height, width: bounds.width, height: newValue)) | |
line.translatesAutoresizingMaskIntoConstraints = false | |
line.backgroundColor = borderColor | |
self.addSubview(line) | |
let views = ["line": line] | |
let metrics = ["lineWidth": newValue] | |
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[line]|", options: [], metrics: nil, views: views)) | |
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