2017-10-06 16 views
0

我已經擴展了UIView類併爲cornerRadius添加了一個屬性。該屬性確實設置爲期望的值。我做了兩個自定義類,一個是從UITextField派生的,另一個是從UILabel派生的。 UITextField獲得圓角,但UILabel不會。UILabel確實獲得roundedCorners

在這方面的任何幫助將不勝感激。

@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} 
    } 

} 

回答

0

我要感謝@rmaddy尋求幫助。我正在寫這個爲所有人的利益。由rmaddy提供的代碼起作用。但是,經過測試,我發現它不是必需的。只需在UIView擴展中設置layer.masksToBounds = true,cornerRadius setter方法就可以實現。所以整個問題都是通過這一行代碼解決的。

所以最終的代碼看起來是這樣的,它的工作原理:

@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} 
    } 

} 

我希望它可以幫助別人也。

2

在你BLabel類您訪問UIView擴展在init方法cornerRadius財產。這是在您有機會設置特定角半徑值之前,因此它將爲0.

BLabelinit方法中沒有指向layer.cornerRadius = cornerRadius的行。只需創建BLabel實例,然後設置它的cornerRadius屬性。

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

實際上,在擴展UIView之後,BLabel的init中的代碼是冗餘的。我只是忘了發表評論。這裏令人費解的部分是UITextField的子類獲得了圓角,BLabel(它是UILabel的子類)沒有得到圓角。 –

+0

更新您的問題以顯示如何創建和設置標籤以及如何創建和設置文本字段。 – rmaddy

0

確保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 
    } 

} 
+0

在'commonInit'中使用'layer.cornerRadius = cornerRadius'是沒有意義的,因爲在這一點上它總是0。 – rmaddy

+0

@rmaddy - 他將它與'UIView @ IBInspectable'擴展一起使用。運作良好(在我的快速測試中)。 – DonMag

+0

爲了澄清我的觀點,通過'init(frame:)'或'init(coder:)'調用它並不會有用。 – rmaddy