swiftuitextviewxcode12ibdesignable

How to make editable values in storyboard using IBDesignable class?


I'm wondering how to create reusable @IBDesignable class for UITextView with which I could change border width, colour, etc. directly from the Storyboard?

Thanks in advance!


Solution

  • You re-route the value to the view's layer, like so:

    @IBDesignable class TextView: UITextView {
        @IBInspectable var borderColor: UIColor? {
            set {
                layer.borderColor = newValue?.cgColor
            }
            get {
                guard let borderColor = layer.borderColor else {
                    return nil
                }
                return UIColor(cgColor: borderColor)
            }
        }
        
        @IBInspectable var borderWidth: CGFloat {
            set {
                layer.borderWidth = newValue
            }
            get {
                return layer.borderWidth
            }
        }
    }