Skip to content

Instantly share code, notes, and snippets.

@bob910078
Last active July 2, 2020 03:42
Show Gist options
  • Save bob910078/eec958b0500ef8051a283f9686077dca to your computer and use it in GitHub Desktop.
Save bob910078/eec958b0500ef8051a283f9686077dca to your computer and use it in GitHub Desktop.
class BBInsetLabel: UILabel {
var textInsets = UIEdgeInsets.zero
override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
guard text != nil else {
return super.textRect(forBounds: bounds, limitedToNumberOfLines: numberOfLines)
}
let insetedRect = bounds.inset(by: textInsets)
let textRect = super.textRect(forBounds: insetedRect, limitedToNumberOfLines: numberOfLines)
let newRect = textRect.inset(by: textInsets.inverted())
return newRect
}
override func drawText(in rect: CGRect) {
let insetedRect = rect.inset(by: textInsets)
super.drawText(in: insetedRect)
}
}
extension UIEdgeInsets {
func inverted() -> UIEdgeInsets {
UIEdgeInsets(top: -top, left: -left, bottom: -bottom, right: -right)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment