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.
Asked
Active
Viewed 474 times
0
-
You can use func placeholderRect(forBounds bounds: CGRect) -> CGRect for placeholder – Sour LeangChhean Mar 17 '17 at 07:59
-
This is for Text: func textRect(forBounds bounds: CGRect) -> CGRect – Sour LeangChhean Mar 17 '17 at 07:59
1 Answers
1
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.

Arpit Jain
- 1,660
- 12
- 23