[swift4] UIlabelやUIbuttonなどに枠線や角丸を付けるExtention

xcodeのStoryBoard上にデフォルトにはUIlabelやUIbuttonなどに枠線や角丸を付けるパラメータは無いが、以下のコードをswiftファイルとして追加すれば、xcodeのStoryBoard上にパラメータが追加される。

あとは視覚的に変更できる。

UIViewExtension.swift

import UIKit

extension UIView {
    @IBInspectable var cornerRadius: CGFloat {
        get {
            return layer.cornerRadius
        }
        set {
            layer.cornerRadius = newValue
            layer.masksToBounds = newValue > 0
        }
    }
    
    @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
        }
    }
}

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です