71

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.

etolstoy
  • 1,788
  • 20
  • 33
  • Marco Arment tweeted a dead-simple `UILabel` subclass that adds `UIEdgeInsets`-style padding in label drawing: it’s on gist.github as [IPInsetLabel](https://gist.github.com/2596057). – cbowns May 25 '12 at 01:41

10 Answers10

109

I am using auto layout. Solution that worked for me was setting UIButton's contentEdgeInsets.

ObjC

button.contentEdgeInsets = UIEdgeInsetsMake(0.0f, 30.0f, 0.0f, 30.0f);

Swift

button.contentEdgeInsets = UIEdgeInsets(top: 0.0, left: 30.0, bottom: 0.0, right: 30.0)
Rafa de King
  • 37,274
  • 22
  • 102
  • 134
22

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.

  • I failed to tweak this solution to work for alignment left. It's a really old answer, I guess it might not work in iOS 6. Hope someone could provide update. – Philip007 Dec 24 '12 at 09:13
  • Works great, but from iOS 6.0 you have to use NSTextAlignmentCenter, UITextAlignmentCenter is deprecated. – chrisdowney Jun 16 '13 at 17:17
22

To set padding in UIButton text you need to use UIButton's contentEdgeInsets property.

In Storyboard, to set content inset do following steps:

  1. Select the view controller, then select the UIButton
  2. Go to Utilities Panel (Command + Option + 0) and select Attributes Inspector (Command + Option + 4)
  3. Now select Content in Edge and set Inset as per your requirement (see screenshot)

enter image description here

Bruno Bieri
  • 7,754
  • 10
  • 55
  • 79
Giru Bhai
  • 14,097
  • 5
  • 43
  • 70
8

To add padding to UILabel the most flexible approach is to subclass UILabel and add an edgeInsets property. You then set the desired insets and the label will be drawn accordingly.

OSLabel.h

#import <UIKit/UIKit.h>

@interface OSLabel : UILabel

@property (nonatomic, assign) UIEdgeInsets edgeInsets;

@end

OSLabel.m

#import "OSLabel.h"

@implementation OSLabel

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if(self){
        self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return self;
}

- (void)drawTextInRect:(CGRect)rect {
    return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)];
}

@end
Brody Robertson
  • 8,107
  • 2
  • 40
  • 42
5

On Xcode 9, the insets are now located in the Size Inspector instead of the Attributes Inspector:

Xcode 9 Size Inspector - Button insets

Alex
  • 1,298
  • 15
  • 30
3

Here's a sublass of UILabel that has customizable padding using an edgeInset property:

PaddedLabel.h

#import <UIKit/UIKit.h>
@interface PaddedLabel : UILabel
@property UIEdgeInsets edgeInsets;
@end

PaddedLabel.m

#import "PaddedLabel.h"
@implementation PaddedLabel
-(void)drawTextInRect:(CGRect)rect {
    return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)];
}
-(CGSize)intrinsicContentSize {
    CGSize contentSize = [super intrinsicContentSize];
    UIEdgeInsets insets = self.edgeInsets;
    contentSize.height += insets.top + insets.bottom;
    contentSize.width += insets.left + insets.right;
    return contentSize;
}
@end

(This is a simplification of Brody's answer which also works with autolayout.)

Jakob Egger
  • 11,393
  • 4
  • 35
  • 47
2

CGRectInset is your friend. You can set negative 'insets' to create padding:

[self sizeToFit];
CGRect rect = self.frame;
rect = CGRectInset( rect, -6.f, -5.f); // h inset, v inset
self.frame = rect;
cleverbit
  • 4,995
  • 4
  • 24
  • 35
0

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 method:

- (CGRect)textRectForBounds:(CGRect)bounds
     limitedToNumberOfLines:(NSInteger)numberOfLines 
Navnath Godse
  • 2,235
  • 2
  • 21
  • 31
Alan Taylor
  • 493
  • 4
  • 16
0

What about creating a custom class that extends UIButton and overriding this:

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    return UIEdgeInsetsInsetRect(contentRect, UIEdgeInsetsMake(topPadding, rightPadding, bottomPadding, leftPadding));
}

In the case of UILabel just override:

- (CGRect)textRectForBounds:(CGRect)bounds 
     limitedToNumberOfLines:(NSInteger)numberOfLines;
PakitoV
  • 2,384
  • 24
  • 34
-2

If you're just looking for a horizontal padding on one line, then this may be enough (it was for me):

NSString* padding = @"  "; // 2 spaces
myLabel.text = [NSString stringWithFormat:@"%@%@%@", padding, name, padding];
Lukas Kalinski
  • 2,095
  • 20
  • 25