2

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

luca
  • 36,606
  • 27
  • 86
  • 125

3 Answers3

3

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

Xcoder
  • 1,762
  • 13
  • 17
1

Try this code....

 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    UIEdgeInsets edgeInsets = button.titleEdgeInsets;
    edgeInsets.left -= 5;
    edgeInsets.right += 5;
    button.titleEdgeInsets = edgeInsets;
Bhanu Prakash
  • 1,493
  • 8
  • 20
0

From xCode, this can be set using the Inset property as shown in the screenshot below: enter image description here

vijayst
  • 20,359
  • 18
  • 69
  • 113