views:

508

answers:

2

Hi,

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

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 label with a background colour, but it seems like overkill.

Many thanks.

+1  A: 

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.

Toby
That's pretty much how you do it.
Dan Ray
A: 

I'm trying to achieve a similar thing, that is 'pad' a UILabel. I've been trying to implement the solution that Toby posted above, but can't seem to find where this needs to go. The UILabel I'm working with is aligned left - is that what's causing the issue?

I've tried using this in viewDidLoad, and even in a subclass of UILabel in the -(CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines method.

Any help?

Alan Taylor