Ios UILabel确实有圆角
我扩展了UIView类,并为cornerRadius添加了一个属性。该属性未设置为所需的值。我创建了两个自定义类,一个派生自UITextField,另一个派生自UILabel。UITextField获得圆角,但UILabel没有 我们将非常感谢您在这方面提供的任何帮助Ios UILabel确实有圆角,ios,swift,uilabel,Ios,Swift,Uilabel,我扩展了UIView类,并为cornerRadius添加了一个属性。该属性未设置为所需的值。我创建了两个自定义类,一个派生自UITextField,另一个派生自UILabel。UITextField获得圆角,但UILabel没有 我们将非常感谢您在这方面提供的任何帮助 @IBDesignable public class BLabel: UILabel { public override init(frame: CGRect) { super.init(frame: frame)
@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}
}
}
在
BLabel
类中,您可以访问init
方法中UIView
扩展名的corneradius
属性。这是在您有机会设置特定的角半径值之前,因此它将为0
在BLabel
的init
方法中,没有指向线layer.cornerRadius=cornerRadius
。只需创建BLabel
实例,然后设置其cornerRadius
属性
let label = BLabel(frame: someFrame)
label.cornerRadius = 5
在
BLabel
类中,您可以访问init
方法中UIView
扩展名的corneradius
属性。这是在您有机会设置特定的角半径值之前,因此它将为0
在BLabel
的init
方法中,没有指向线layer.cornerRadius=cornerRadius
。只需创建BLabel
实例,然后设置其cornerRadius
属性
let label = BLabel(frame: someFrame)
label.cornerRadius = 5
您确定您的UITextField
正在响应corneradius
?或者你只是看到了正常的圆角
尝试将您的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
}
}
您确定您的UITextField
正在响应corneradius
?或者你只是看到了正常的圆角
尝试将您的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
}
}
我要感谢@rmaddy的帮助。我写这篇文章是为了所有人的利益。rmaddy给出的代码有效。但是,经过测试,我发现它不是必需的。只需在UIView extension 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}
}
}
我希望它也能帮助其他人。我要感谢@rmaddy的帮助。我写这篇文章是为了所有人的利益。rmaddy给出的代码有效。但是,经过测试,我发现它不是必需的。只需在UIView extension 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}
}
}
我希望它也能帮助其他人。实际上,在我扩展UIView之后,BLabel的init中的代码是多余的。我只是忘了评论那件事。这里令人费解的是UITextField的子类得到圆角,而BLabel(UILabel的子类)没有得到圆角。请更新您的问题,以显示如何创建和设置标签以及如何创建和设置文本字段。实际上,在扩展UIView后,BLabel的init中的代码是多余的。我只是忘了评论那件事。这里令人费解的是UITextField的子类得到圆角和BLabel(UILabel的子类)没有圆角。请更新您的问题以显示如何创建和设置标签以及如何创建和设置文本字段。在
commonInit
中使用layer.cornerRadius=cornerRadius
是毫无意义的,因为此时它将始终为0。@rmaddy-他正在将其与ui视图一起使用@i可检测
扩展名。(在我的快速测试中)效果很好。为了澄清我的观点,当通过init(frame:)
或init(coder:)
@rmaddy调用它时,它不会有用-你完全正确-我的错。答案已更新。Thx@rmaddy:您的代码运行良好。我通过消除每个覆盖对其进行了测试,发现PrepareForenterFaceBuilder()正在发挥作用。但后来我意识到,我应该尝试在UIView扩展中设置layer.masksToBounds=true,它的效果非常好。我删除了所有其他方法。为了方便大家,在UIView扩展中设置layer.masksToBounds=true可以解决这个问题。非常感谢rmaddy的帮助。在commonInit
中使用layer.cornerRadius=cornerRadius
是毫无意义的,因为此时它将始终为0。@rmaddy-他正在与UIView@IBInspectable
扩展一起使用它。(在我的快速测试中)效果很好。为了澄清我的观点,当通过init(frame:)
或init(coder:)
@rmaddy调用它时,它不会有用-你完全正确-我的错。答案已更新。Thx@rmaddy:您的代码运行良好。我通过消除每个覆盖对其进行了测试,发现PrepareForenterFaceBuilder()正在发挥作用。但后来我意识到,我应该尝试在UIView扩展中设置layer.masksToBounds=true,它的效果非常好。我删除了所有其他方法。为了方便大家,在UIView扩展中设置layer.masksToBounds=true可以解决这个问题。非常感谢你的帮助。