Last active
September 23, 2016 05:41
-
-
Save NikolaiRuhe/9dd52c0ff7359279d6744fac54cc625b to your computer and use it in GitHub Desktop.
A UILabel subclass that adds padding around the text and handles layout properly.
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 | |
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) { | |
super.drawTextInRect(textInsets.apply(rect)) | |
} | |
} | |
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