Bordered UILabel

I was looking around on the internet for how to put a border around the text of a UILabel and found this post on stackoverflow that solved my problem. I decided to turn that solution into a UILabel subclass and share it here. Big thanks to kprevas for providing the answer!


#import <UIKit/UIKit.h>

@interface SNBorderdLabel : UILabel
   UIColor* _borderColor;
   int _borderSize;

@property (nonatomic, retain) UIColor* borderColor;
@property (nonatomic) int borderSize;



#import "SNBorderdLabel.h"

@implementation SNBorderdLabel
@synthesize borderColor = _borderColor;
@synthesize borderSize = _borderSize;

- (id)initWithFrame:(CGRect)frame
   self = [super initWithFrame:frame];
   if (self) {
      _borderColor = [[UIColor whiteColor] retain];
      _borderSize = 1;


   return self;

- (void)drawTextInRect:(CGRect)rect {

   CGSize shadowOffset = self.shadowOffset;
   UIColor *textColor = self.textColor;

   CGContextRef c = UIGraphicsGetCurrentContext();
   CGContextSetLineWidth(c, self.borderSize);

   CGContextSetLineJoin(c, kCGLineJoinRound);
   CGContextSetTextDrawingMode(c, kCGTextStroke);
   self.textColor = self.borderColor;
   [super drawTextInRect:rect];

   CGContextSetTextDrawingMode(c, kCGTextFill);
   self.textColor = textColor;
   self.shadowOffset = CGSizeMake(0, 0);
   [super drawTextInRect:rect];

   self.shadowOffset = shadowOffset;


Here’s how to use it:

   SNBorderdLabel* textLabel = [[SNBorderdLabel alloc] init];
   textLabel.borderColor = [UIColor whiteColor];
   textLabel.borderSize = 2;

Hope that this helps you as much as it did me!

2 Responses to Bordered UILabel

  1. Jake Spencer says:

    Thanks a lot! I had looked at some SO answers and couldn’t get this working just right. Your solution showed me all the details I had trouble figuring out on my own!

  2. Awesome! I’m glad to hear it. Let me know if there is anything in particular you want me to post about.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: