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

user210437 picture user210437 · Jun 16, 2010 · Viewed 57.6k times · Source

I've got various views in my iPhone application that require padding e.g a custom UIButton with text aligned left, and a UILabel with a background color.

This may be a really stupid question, but how can I apply 'padding' to move the text off the left hand edge?

I've tired using bounds etc without any success.

I know I could create a wrapper view for my UILabel with a background color, but it seems like overkill.

Many thanks.

Answer

RaffAl picture RaffAl · May 9, 2014

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)