Search code examples
iosswift3uitextfieldibdesignableibinspectable

How can I make a custom subclass of uitextfield and over there using @IBDesignable & @IBInspectable I can set placholder padding from left


I want to make a subclass of UITextField and over there by using @IBDesignable and @IBInspectable set the left side as well as right side padding for the text as well as placeholder. Can ignore left and right view.


Solution

  • Create a custom class for your textfield and give your textfield class as CustomTextField.

    class CustomTextField: UITextField {
    
      @IBInspectable var padding : CGFloat = 0 {
          didSet{
                  let paddingView : UIView = UIView(frame: CGRect(x: 0, y: 0, width: padding, height: self.frame.size.height))
                  self.leftView = paddingView
                  self.rightView = paddingView
                  self.leftViewMode = .always
        }
      }
     }
    

    Then, you can set padding from storyboard. And placeholder can be set from Attributes Inspector in storyboard.