ssantos ssantos - 10 months ago 73
iOS Question

iOS Add left padding to UILabel

I need to create a

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?


A screenshot to illustrate my scenario.-

enter image description here


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)];