IOS 6 - 自定义UIButton如何给我的titleLabel一个保证金?

时间:2013-03-19 09:52:20

标签: ios objective-c cocoa-touch uibutton drawrect

我在drawRect:方法中为我的UIButton子类绘制了一个嵌入的矩形边框。 正如您所看到的那样,形状太靠近我的按钮titleLabel框架。如何设置titleLabel的最大宽度/边距以避免这种情况?

DrawRect:方法

- (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

3 个答案:

答案 0 :(得分:3)

在drawRect方法中使用self.titleEdgeInsets=UIEdgeInsetsMake(0, 5, 0, 5);

答案 1 :(得分:1)

试试这个代码......

 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    UIEdgeInsets edgeInsets = button.titleEdgeInsets;
    edgeInsets.left -= 5;
    edgeInsets.right += 5;
    button.titleEdgeInsets = edgeInsets;

答案 2 :(得分:0)

从xCode,可以使用Inset属性设置,如下面的屏幕截图所示: enter image description here