IOS

属性拓展

2019-01-04  本文已影响4人  瑟闻风倾
  1. UIViewHelper:拓展UIView
//
//  UIViewHelper.swift
//  JackUChat
//
//  Created by 徐云 on 2018/12/26.
//  Copyright © 2018 Liy. All rights reserved.
//

import UIKit

extension UIView{
    
    //线宽:@IBInspectable修饰的属性,能够让属性在styoyboard的属性栏进行显示
    @IBInspectable
    var borderWidth: CGFloat {
        get {
            return self.layer.borderWidth
        }
        set {
            self.layer.borderWidth = newValue
        }
    }
    //颜色
    @IBInspectable
    var borderColor: UIColor {
        get {
            return UIColor(cgColor: self.layer.borderColor!)
        }
        set {
            self.layer.borderColor  = newValue.cgColor
        }
    }
    //圆角半径
    @IBInspectable
    var cornerRadius: CGFloat {
        get {
            return self.layer.cornerRadius
        }
        set {
            self.layer.cornerRadius = newValue
            self.layer.masksToBounds = newValue > 0
        }
    }
 

    //圆角半径
//    @IBInspectable
//    var cornerRadius: CGFloat {
//        get {
//            return layer.cornerRadius
//        }
//        set {
//            layer.cornerRadius = newValue
//        }
//    }
    
    
    //阴影:圆角,透明度,颜色,偏移
    @IBInspectable
    var shadowRadius: CGFloat {
        get {
            return layer.shadowRadius
        }
        set {
            layer.shadowRadius = newValue
        }
    }
    @IBInspectable
    var shadowOpacity: Float {
        get {
            return layer.shadowOpacity
        }
        set {
            layer.shadowOpacity = newValue
        }
    }
    @IBInspectable
    var shadowColor: UIColor? {
        get {
            return layer.shadowColor != nil ? UIColor.init(cgColor: layer.shadowColor!) : nil
        }
        set {
            layer.shadowColor = newValue?.cgColor
        }
    }
    @IBInspectable
    var shadowOffset: CGSize {
        get {
            return layer.shadowOffset
        }
        set {
            layer.shadowOffset = newValue
        }
    }
    
    
}

备注:在故事版storyboard中选中任意控件如ImageView,则在属性栏中出现拓展的相关属性,并可以直接设置.


image.png
  1. ColorHelper:拓展UIColor
//
//  ColorHelper.swift
//  JackUChat
//
//  Created by 徐云 on 2018/12/28.
//  Copyright © 2018 Liy. All rights reserved.
//

extension UIColor{
    static  var  jackColor : UIColor{
        return UIColor(red: 6/255, green: 167/255, blue: 229/255, alpha: 1)
    }
    
}
上一篇下一篇

猜你喜欢

热点阅读