【问题标题】:IOS 6 - Custom UIButton how to give my titleLabel a margin?IOS 6 - 自定义 UIButton 如何给我的 titleLabel 留出余量?
【发布时间】:2013-03-19 09:52:19
【问题描述】:

我在 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);

}

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      试试这个代码....

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

      【讨论】:

        【解决方案3】:

        在 xCode 中,可以使用 Inset 属性进行设置,如下面的屏幕截图所示:

        【讨论】:

          猜你喜欢
          • 2020-05-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-27
          • 1970-01-01
          • 1970-01-01
          • 2012-07-08
          • 2014-01-04
          相关资源
          最近更新 更多