【问题标题】:UILabel does get roundedCornersUILabel 确实得到了圆角
【发布时间】:2017-10-06 15:53:54
【问题描述】:

我扩展了 UIView 类并为cornerRadius 添加了一个属性。该属性确实设置为所需的值。我制作了两个自定义类,一个来自 UITextField,另一个来自 UILabel。 UITextField 得到圆角,但 UILabel 没有。

我们将非常感谢您在这方面的任何帮助。

@IBDesignable
public class BLabel: UILabel {


public override init(frame: CGRect) {
    super.init(frame: frame)
    layer.cornerRadius = cornerRadius
    layer.masksToBounds = true
    clipsToBounds = true

}


required public init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    }
}

extension UIView {
@IBInspectable
var cornerRadius : CGFloat {
    get {return layer.cornerRadius}
    set {layer.cornerRadius = newValue}
    }

}

【问题讨论】:

    标签: ios swift uilabel


    【解决方案1】:

    BLabel 类中,您可以在init 方法中访问UIView 扩展的cornerRadius 属性。这是在您有机会设置特定的拐角半径值之前,因此它将为 0。

    BLabelinit 方法中的 layer.cornerRadius = cornerRadius 行没有意义。只需创建 BLabel 实例,然后设置其 cornerRadius 属性。

    let label = BLabel(frame: someFrame)
    label.cornerRadius = 5
    

    【讨论】:

    • 其实BLabel的init中的代码在我扩展UIView后是多余的。我只是忘了评论。这里令人费解的是,UITextField 的子类得到了圆角,而 BLabel(它是 UILabel 的子类)没有得到圆角。
    • 更新您的问题以显示您如何创建和设置标签以及如何创建和设置文本字段。
    【解决方案2】:

    确定您的UITextField 正在回复cornerRadius 吗?或者您可能只是看到正常的圆角?

    尝试将您的 BLabel 更改为此 - 它将确保正确调用初始化:

    @IBDesignable
    public class BLabel: UILabel {
    
    
        public override init(frame: CGRect) {
            super.init(frame: frame)
            commonInit()
        }
    
        required public init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            commonInit()
        }
    
        public override func awakeFromNib() {
            super.awakeFromNib()
            commonInit()
        }
    
        public override func prepareForInterfaceBuilder() {
            super.prepareForInterfaceBuilder()
            commonInit()
        }
    
        func commonInit() {
    
            // As noted by "rmaddy" ---
            // setting .cornerRadius here does nothing, as it is always equal to Zero
            // the UIView extension will handle it
            //layer.cornerRadius = cornerRadius
    
            layer.masksToBounds = true
            clipsToBounds = true
    
            // the following just makes it easy to confirm
            // that this code is being executed
            backgroundColor = UIColor.red
            textColor = UIColor.yellow
            textAlignment = .center
        }
    
    }
    

    【讨论】:

    • commonInit 中有layer.cornerRadius = cornerRadius 毫无意义,因为此时它始终为0。
    • @rmaddy - 他将它与UIView @IBInspectable 扩展一起使用。效果很好(在我的快速测试中)。
    • 为了澄清我的观点,当通过init(frame:)init(coder:) 调用时它不会有用。
    • @rmaddy - 你是绝对正确的 - 我的错。答案已更新。谢谢
    • @rmaddy:您的代码运行良好。我通过消除每个覆盖对其进行了测试,发现 prepareForInterfaceBuilder() 起到了作用。但后来我意识到我应该尝试在 UIView 扩展中设置 layer.masksToBounds = true ,它就像一个魅力。我删除了所有其他方法。只是为了在 UIView 扩展中设置 layer.masksToBounds = true 解决问题。非常感谢 rmaddy 的帮助。
    【解决方案3】:

    感谢@rmaddy 的帮助。我写这篇文章是为了大家的利益。 rmaddy 给出的代码有效。但是,经过测试,我发现它不是必需的。只需在 UIView 扩展cornerRadius setter 方法中设置 layer.masksToBounds = true 就可以了。所以整个问题只用了这行代码就解决了。

    所以最终的代码看起来像这样并且它可以工作:

    @IBDesignable
    public class BTextField: UITextField {
    
    
        public override init(frame: CGRect) {
            super.init(frame: frame)
    
        }
    
        required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    
    
        }
    }
    
    
    @IBDesignable
    public class BLabel: UILabel {
    
    
        public override init(frame: CGRect) {
            super.init(frame: frame)
    
        }
    
        required public init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
    
        }
    
    
    }
    
    
    
    extension UIView {
         @IBInspectable
        var cornerRadius : CGFloat {
            get {return layer.cornerRadius}
            set {layer.cornerRadius = newValue
            layer.masksToBounds = true}
        }
    
    }
    

    我希望它也可以帮助其他人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-04
      • 1970-01-01
      • 1970-01-01
      • 2010-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多