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.
I am using auto layout. Solution that worked for me was setting
UIButton
'scontentEdgeInsets
.ObjC
Swift
Here's a sublass of UILabel that has customizable padding using an edgeInset property:
PaddedLabel.h
PaddedLabel.m
(This is a simplification of Brody's answer which also works with autolayout.)
Ok the simplest solution I've found so far is:
Not exactly what I wanted, but it works.
On Xcode 9, the insets are now located in the Size Inspector instead of the Attributes Inspector:
What about creating a custom class that extends UIButton and overriding this:
In the case of UILabel just override:
If you're just looking for a horizontal padding on one line, then this may be enough (it was for me):