简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM