Skip to content

Instantly share code, notes, and snippets.

@hintoz
Created July 18, 2018 15:46
Show Gist options
  • Save hintoz/33733200b7d00b82d44df755784ccb91 to your computer and use it in GitHub Desktop.
Save hintoz/33733200b7d00b82d44df755784ccb91 to your computer and use it in GitHub Desktop.
class BorderedLabel: UILabel {
@IBInspectable var topInset: CGFloat = 0.0
@IBInspectable var bottomInset: CGFloat = 0.0
@IBInspectable var leftInset: CGFloat = 5.0
@IBInspectable var rightInset: CGFloat = 5.0
@IBInspectable var color: UIColor = .white
override func drawText(in rect: CGRect) {
let insets = UIEdgeInsets.init(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
}
override var intrinsicContentSize: CGSize {
get {
var contentSize = super.intrinsicContentSize
contentSize.height += topInset + bottomInset
contentSize.width += leftInset + rightInset
return contentSize
}
}
override func awakeFromNib() {
layer.borderWidth = 1.0
layer.cornerRadius = 8
layer.borderColor = color.cgColor
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment