Search code examples
iosobjective-ccocoa-touchuibuttondrawrect

IOS 6 - Custom UIButton how to give my titleLabel a margin?


I'm drawing an inset rectangular border to my UIButton subclass inside the drawRect: method. As you can see that shape is too close to my button titleLabel frame. How can I set the titleLabel's max width/margin to avoid this?

DrawRect: method

- (void)drawRect:(CGRect)rect {
[super drawRect:rect];

  CGContextRef context = UIGraphicsGetCurrentContext();

  CGContextSetStrokeColorWithColor(context, [UIColor bluecolor].CGColor);

CGContextSetLineWidth(context, 2.0);

CGContextMoveToPoint(context, 4,4);

CGContextAddLineToPoint(context, 56, 4);
CGContextAddLineToPoint(context, 56, 56);
CGContextAddLineToPoint(context, 4, 56);
CGContextAddLineToPoint(context, 4,3);

// and now draw the Path!
CGContextStrokePath(context);

}

This is the button


Solution

  • Use self.titleEdgeInsets=UIEdgeInsetsMake(0, 5, 0, 5); in drawRect method.