UIlabel图层的cornerRadius在iOS 7.1中无效。

198

我目前正在查看一个具有属性 addMessageLabel.layer.cornerRadius = 5.0f; 的UILabel。 在安装了iOS 7.0的设备上,它的角是圆形的。 在安装了iOS 7.1的设备上,它的角不是圆形的。

这只是iOS 7.1的一个错误吗?

8个回答

519

将属性clipsToBounds设置为true

addMessageLabel.clipsToBounds = true

3
不确定为什么在iOS 7上不需要这样做,但在iOS 7.1上却需要,但它起作用了!谢谢。 - mverderese
11
不,不是奇怪...只是“进步”...<哼>,似乎UILabel的clipsToBounds现在默认为FALSE,就像大多数其他UIView一样。苹果可能在努力使东西更加一致。我也遇到了同样的问题。 - Leslie Godwin
2
@ChristopherKing 我找不到关于这个的文档,但在我的情况下也起作用了,有点惊喜 :) - Raheel Sadiq
3
感谢 Stack Overflow 的开发者。 - scrrr
1
@imankazemayni 这篇帖子有点旧了,它的答案是用 Objective C 回答的。Objective C 中使用 YES/NO 表示 true/false,我也会更新我的回答。 - Raheel Sadiq
显示剩余4条评论

67

我认为设置圆角的最佳方式是:

图片描述

并确保勾选“剪裁子视图”:

图片描述

勾选“剪裁子视图”等同于代码addMessageLabel.clipsToBounds = YES;


肯定,最简单的方法 - Mário Carvalho
这是最好的方法。已在iOS 8+和Xcode 7.2上进行了测试。 - lifeisfoo
如果有人来到这里寻找答案,但是它没有起作用:如果您设置了属性“cornerRadius”,它将在iOS10+上工作。在iOS9上,它必须是“layer.cornerRadius”。 - Nathan Barreto
clipToBounds也可以在用户定义的运行时属性中设置(与layer.cornerRadius相同):它应该是关键路径:clipsToBounds,类型:布尔值,值:true。 - Chuy47

31

请尝试以下方法,

[[addMessageLabel layer] setCornerRadius:5.0f];
[[addMessageLabel layer] setMasksToBounds:YES];

//or
[addMessageLabel setClipsToBounds:YES];

Swift

addMessageLable.layer.cornerRadius = 5.0
addMessageLable.layer.masksToBounds = true

//or
addMessageLable.layer.clipsToBounds = true

5

我的问题有些不同。

虽然我确实使用了btn.clipsToBounds = true

但我没有设置:

btn.layer.cornerRadius = 20

因为我有不同的屏幕尺寸。所以我按照这个回答的方法进行了操作:

override func layoutSubviews() {
    seeMoreButton.layer.cornerRadius = seeMoreButton.bounds.size.height / 2
}

由于我忘记添加 super.layoutSubviews(),所以它没有起作用。 正确的代码是:
override func layoutSubviews() {
    super.layoutSubviews()
    seeMoreButton.layer.cornerRadius = seeMoreButton.bounds.size.height / 2
}

非常感谢,只有这个答案对我有效。Swift 3,Xcode 8.3.3。 - Skywalker

3
我尝试了下面的方法并获得了成功的输出结果。
yourlabelname.layer.cornerRadius = 10.0f;
[yourlabelname setClipsToBounds:YES];

还有什么其他事情阻止了您吗?


(涉及 IT 技术问题)

在iOS 7.1之前,clipsToBounds的默认值为YES,因此我的原始代码中没有这行代码[yourlabelname setClipsToBounds:YES]; - mverderese

0
 //works perfect in Swift 2.0 for a circular or round image          


@IBOutlet var theImage: UIImageView!
        override func viewDidLoad() {
            super.viewDidLoad()
    //Make sure the width and height are same
            self.theImage.layer.cornerRadius = self.theImage.frame.size.width / 2
            self.theImage.layer.borderWidth = 2.0
            self.theImage.layer.borderColor = UIColor.whiteColor().CGColor
            self.theImage.clipsToBounds = true

        }

0
yourlabelname.layer.cornerRadius = yourlabelname.frame.size.width/2;
[yourlabelname setClipsToBounds:YES];

请确保您正在与适当的部署目标进行检查。


0
将以下代码作为UIView的扩展添加。
//// Story board Extra Feature for create border radius, border width and border Color
extension UIView {
    /// corner radius
    @IBInspectable var borderColor: UIColor? {
        set {
            layer.borderColor = newValue!.cgColor
        }
        get {
            if let color = layer.borderColor {
                return UIColor(cgColor: color)
            } else {
                return nil
            }
        }
    }
    @IBInspectable var borderWidth: CGFloat {
        set {
            layer.borderWidth = newValue
        }
        get {
            return layer.borderWidth
        }
    }
    @IBInspectable var cornerRadius: CGFloat {
        set {
            layer.cornerRadius = newValue
            clipsToBounds = newValue > 0
        }
        get {
            return layer.cornerRadius
        }
    }
}

之后,您将在接口生成器中获得以下属性。

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接