How to achieve UIButton / UILabel 'padding' in iPhone app

To set padding in UIButton text you need to use UIButton's contentEdgeInsets property.

In Storyboard, to set content inset do following steps:

  1. Select the view controller, then select the UIButton
  2. Go to Utilities Panel (Command + Option + 0) and select Attributes Inspector (Command + Option + 4)
  3. Now select Content in Edge and set Inset as per your requirement (see screenshot)

enter image description here


Ok the simplest solution I've found so far is:

self.textAlignment = UITextAlignmentCenter; 
[self sizeToFit]; 
CGRect frame = self.frame;
frame.size.width += 20; //l + r padding 
self.frame = frame;

Not exactly what I wanted, but it works.


I am using auto layout. Solution that worked for me was setting UIButton's contentEdgeInsets.

ObjC

button.contentEdgeInsets = UIEdgeInsetsMake(0.0f, 30.0f, 0.0f, 30.0f);

Swift

button.contentEdgeInsets = UIEdgeInsets(top: 0.0, left: 30.0, bottom: 0.0, right: 30.0)