2

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

绘制矩形:方法

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

}

这是按钮

4

3 回答 3

3

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

于 2013-03-19T10:00:09.673 回答
1

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

 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    UIEdgeInsets edgeInsets = button.titleEdgeInsets;
    edgeInsets.left -= 5;
    edgeInsets.right += 5;
    button.titleEdgeInsets = edgeInsets;
于 2013-03-19T10:03:21.337 回答
0

在 xCode 中,可以使用 Inset 属性进行设置,如下面的屏幕截图所示: 在此处输入图像描述

于 2014-11-26T02:15:01.820 回答