By default, you can’t pad or add margins to a UILabel. You have to apply custom insets (fancy for “margins”) in order to do so, and it’s repetitive and annoying in practice.

So I built a custom class – UIBorderLabel – to automate the process. You treat it just like a regular UILabel, except you can specify a numeric value for each margin.

Here’s the header file:

#import

@interface UIBorderLabel : UILabel
{
    CGFloat topInset;
    CGFloat leftInset;
    CGFloat bottomInset;
    CGFloat rightInset;
}

@property (nonatomic) CGFloat topInset;
@property (nonatomic) CGFloat leftInset;
@property (nonatomic) CGFloat bottomInset;
@property (nonatomic) CGFloat rightInset;

@end


And the corresponding implementation:

#import "UIBorderLabel.h"

@implementation UIBorderLabel

@synthesize topInset, leftInset, bottomInset, rightInset;

– (void)drawTextInRect:(CGRect)rect
{
    UIEdgeInsets insets = {self.topInset, self.leftInset,
        self.bottomInset, self.rightInset};

    return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}

@end


To use, specify the insets immediately after you create the UIBorderLabel:

UIBorderLabel *myBorderLabel = [[UIBorderLabel alloc] init];

myBorderLabel.topInset = 10;
myBorderLabel.leftInset = 15;
myBorderLabel.bottomInset = 10;
myBorderLabel.rightInset = 15;


Here’s the result. In the example below, “Relax” is a UIBorderLabel with a 20-pixel margin on all sides.


UILabel with Custom Insets

(screenshot is from Travel Rules, and used with implicit permission since I wrote it)😉