【问题标题】:iOS: UIButton draw circle border in iOSiOS:iOS中的UIButton绘制圆形边框
【发布时间】:2016-08-12 09:28:53
【问题描述】:

我需要在 UIButton 外面画一个像边框一样的圆圈。以下是附加图像和代码。我还需要在按钮下方显示文本。以下代码将在其下方添加图像和文本。但是我怎么有层。

   UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [imageButton setBackgroundImage:image forState:UIControlStateNormal];
    imageButton.translatesAutoresizingMaskIntoConstraints = NO;
    imageButton.backgroundColor = [UIColor clearColor];
    [imageButton setTitle:title forState:UIControlStateNormal];
    imageButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    imageButton.titleLabel.font = [UIFont fontWithName:@"OpenSans" size:fon];
    [imageButton setTitleEdgeInsets:UIEdgeInsetsMake(95, 0.0f, 0.0f, 0.0f)];
    [imageButton setTitleColor:[UIColor iconTextColor] forState:UIControlStateNormal];
    [imageButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

【问题讨论】:

  • this 教程的一部分可能会有所帮助。
  • 查看 CAShapeLayer 和 +[UIBezierPath bezierPathWithArcCenter: radius: startAngle: endAngle: clockwise: ]

标签: ios objective-c iphone uibutton


【解决方案1】:

设置按钮的宽度和高度例如:宽度 = 100 和高度 = 100,如果你想要一个圆形,应该相同,

imageButton.layer.cornerRadius = 50 // this value should be half from width or height (width = height)
imageButton.layer.masksToBounds = YES

//如果你想要与上面的边框

 imageButton.layer.borderWidth = 1;
 imageButton.layer.borderColor = [[UIColor blackColor] CGColor];

【讨论】:

  • 这不起作用,因为我之前会以圆形格式获取图像,我在该图像下方添加了文本。现在主要关注的是在边框中绘制的图像外部添加颜色。这种边框颜色不起作用。
  • 这将做一个 360° 的轮廓,而不是像图像中的 180°
  • 在你的情况下,你可以使用uiview --> 然后在视图内 (uiimageview and uilabel) ---> 然后 unbutton
  • @Dopapp 是的,绝对正确,我们该如何处理,因为在 uibutton 下方添加文本后,它会给出 360° 边框颜色
  • 为什么不能使用带边框的图片
【解决方案2】:

你可以这样做。

borderView = UIView()

yourButton = UIButton()

borderView.frame.size.width = yourButton.frame.size.width + 1

borderView.layer.cornerRadius = borderView.frame.size.width/2

borderView.clipToBounds = true

borderView.backgroundColor = UIColor.clearColor()

borderView.layer.borderWidth = 1

borderView.layer.borderColor = UIColor.blueColor()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 2019-06-06
    相关资源
    最近更新 更多