Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A UILabel subclass that adds padding around the text and handles layout properly.
import UIKit
class NRLabel : UILabel {
var textInsets: UIEdgeInsets = UIEdgeInsetsZero {
didSet { invalidateIntrinsicContentSize() }
override func textRectForBounds(bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
var rect = textInsets.apply(bounds)
rect = super.textRectForBounds(rect, limitedToNumberOfLines: numberOfLines)
return textInsets.inverse.apply(rect)
override func drawTextInRect(rect: CGRect) {
extension UIEdgeInsets {
var inverse: UIEdgeInsets {
return UIEdgeInsets(top: -top, left: -left, bottom: -bottom, right: -right)
func apply(rect: CGRect) -> CGRect {
return UIEdgeInsetsInsetRect(rect, self)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.