Skip to content

Instantly share code, notes, and snippets.

@dataxpress
Last active April 6, 2020 18:47
Show Gist options
  • Save dataxpress/a4b9bd5ea57acb80e0d512cc9e577b18 to your computer and use it in GitHub Desktop.
Save dataxpress/a4b9bd5ea57acb80e0d512cc9e577b18 to your computer and use it in GitHub Desktop.
a uilabel with edge padding borrowed from stackoverflow
import UIKit
@IBDesignable class PaddedLabel: UILabel {
@IBInspectable var topInset: CGFloat = 0.0
@IBInspectable var bottomInset: CGFloat = 0.0
@IBInspectable var leftInset: CGFloat = 0.0
@IBInspectable var rightInset: CGFloat = 0.0
func setAllInsets(_ inset : CGFloat){
topInset = inset
bottomInset = inset
leftInset = inset
rightInset = inset
}
override func drawText(in rect: CGRect) {
let insets = UIEdgeInsets.init(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
super.drawText(in: rect.inset(by: insets))
}
override var intrinsicContentSize: CGSize {
let size = super.intrinsicContentSize
return CGSize(width: min(size.width + leftInset + rightInset, 10000),
height: size.height + topInset + bottomInset)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment