Swift 扩展 Storyboard 属性

266 阅读1分钟

可以在 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
        }
    }
}

文件下载地址