Search code examples
iphoneuiviewuibuttonuilabelpadding

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


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.


Solution

  • 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.