Last active
April 6, 2020 18:47
-
-
Save dataxpress/a4b9bd5ea57acb80e0d512cc9e577b18 to your computer and use it in GitHub Desktop.
a uilabel with edge padding borrowed from stackoverflow
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 | |
@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