user3662992
user3662992

Reputation: 688

Adding a space before and after the text in UILabel using the storyboard

How to add a space before and after the text in UILabel using storyboard.

Here is an example of label with a background.

enter image description here

Upvotes: 0

Views: 1260

Answers (1)

matt
matt

Reputation: 534925

One approach is:

  • Use auto layout in the storyboard.

  • Use a UILabel subclass that overrides intrinsicContentSize to be a little wider than the default.

For example:

extension CGSize {
    func sizeByDelta(dw dw:CGFloat, dh:CGFloat) -> CGSize {
        return CGSizeMake(self.width + dw, self.height + dh)
    }
}

class MyWiderLabel : UILabel {
    override func intrinsicContentSize() -> CGSize {
        return super.intrinsicContentSize().sizeByDelta(dw: 20, dh: 0)    
    }
}

Now just set the class of every label in your storyboard to be a MyWiderLabel.

Upvotes: 2

Related Questions