Last active
September 22, 2018 16:43
-
-
Save nanoxd/07b6f023c8036fdf92dfa0e380f5e981 to your computer and use it in GitHub Desktop.
[UIInsetLabel] A custom UILabel that can be inset #swift #uikit
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
/// An UILabel that allows insetting the label | |
class UIInsetLabel: UILabel { | |
/// The insets to inset the main `rect` by | |
var insets: UIEdgeInsets | |
init(insets: UIEdgeInsets) { | |
self.insets = insets | |
super.init(frame: .zero) | |
} | |
required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } | |
override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect { | |
let insetRect = UIEdgeInsetsInsetRect(bounds, insets) | |
let textRect = super.textRect(forBounds: insetRect, limitedToNumberOfLines: numberOfLines) | |
let invertedInsets = UIEdgeInsets(top: -insets.top, left: -insets.left, bottom: -insets.bottom, right: -insets.right) | |
return UIEdgeInsetsInsetRect(textRect, invertedInsets) | |
} | |
override func drawText(in rect: CGRect) { | |
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment