Search code examples
iosswiftuilabelnsattributedstring

Add top and bottom padding to UILabel using Swift


How do you add padding to the top and bottom of the content within a UILabel?

I have tried to find the means of doing it via NSAttributedString text but cannot seem to find a way to apply margins to the top and bottom only.


Solution

  • Do this in swift :-

    class Label: UILabel {
        override func drawTextInRect(rect: CGRect) {
            super.drawTextInRect(UIEdgeInsetsInsetRect(rect, UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)))
        }
    }