Swift 擴充套件 Storyboard 屬性

Brocadecarp發表於2018-04-23

可以在 Storyboard 中直接設定 UI 控制元件的基本樣式。

import UIKit

//@IBDesignable
extension UIView {

    @IBInspectable var cornerRadius:CGFloat {
        set{
            self.layer.cornerRadius = newValue
            self.layer.masksToBounds = true
        }
        
        get{
            return self.layer.cornerRadius
        }
    }
    
    @IBInspectable var borderWidth:CGFloat {
        set{
            self.layer.borderWidth = newValue
        }
        get{
            return self.layer.borderWidth
        }
    }
    
    @IBInspectable var boderColor:UIColor {
        set{
            self.layer.borderColor = newValue.CGColor
        }
        get{
            return UIColor(CGColor: self.layer.borderColor!)
        }
    }
    
    @IBInspectable var shadowColor:UIColor{
        set{
            self.layer.shadowColor = newValue.CGColor
        }
        get{
             return UIColor(CGColor: self.layer.shadowColor!)
        }
    }
    
    @IBInspectable var shadowOffset:CGSize{
        set{
            self.layer.shadowOffset = newValue
        }
        get{
            return self.layer.shadowOffset
        }
    }
    
    @IBInspectable var shadowOpacity:Float{
        set{
            self.layer.shadowOpacity = newValue
        }
        get{
            return self.layer.shadowOpacity
        }
    }
}
複製程式碼

檔案下載地址

相關文章