Last active
July 2, 2020 03:42
-
-
Save bob910078/eec958b0500ef8051a283f9686077dca to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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