ssantos ssantos - 6 months ago 35
iOS Question

iOS Add left padding to UILabel

I need to create a

UILabel
with a background color, and I'd like to add some left padding but every solution I've found around here seems like a nasty hack.

Which is the 'standard' way to achieve this from iOS 5 ahead?

EDIT

A screenshot to illustrate my scenario.-

enter image description here

Answer

For a full list of available solutions, see this answer: UILabel text margin


Try subclassing UILabel, like @Tommy Herbert suggests in the answer to [this question][1]. Copied and pasted for your convenience:

I solved this by subclassing UILabel and overriding drawTextInRect: like this:

- (void)drawTextInRect:(CGRect)rect {
    UIEdgeInsets insets = {0, 5, 0, 5};
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}