I was using this post as reference: Create space at the beginning of a UITextField. And in this post, there is a very helpful class adding padding in a textfield. However, the only way that I know how to use this class is for me to programmatically create a textfield. But instead, I would like to use this class with an IBOutlet. Here is the TextField Class:
class TextField: UITextField {
let padding = UIEdgeInsets(top: 0, left: 35, bottom: 0, right: 5);
override func textRectForBounds(bounds: CGRect) -> CGRect {
return self.newBounds(bounds)
}
override func placeholderRectForBounds(bounds: CGRect) -> CGRect {
return self.newBounds(bounds)
}
override func editingRectForBounds(bounds: CGRect) -> CGRect {
return self.newBounds(bounds)
}
private func newBounds(bounds: CGRect) -> CGRect {
print("paisjdpfij")
var newBounds = bounds
newBounds.origin.x += padding.left
newBounds.origin.y += padding.top
newBounds.size.height -= padding.top + padding.bottom
newBounds.size.width -= padding.left + padding.right
return newBounds
}
}
And here is my attempt to use it with my IBOutlet:
@IBOutlet weak var firstNameTextField: TextField!
override func viewDidLoad() {
super.viewDidLoad()
firstNameTextField = TextField()
}
However, the there is still no padding in the textfield. Anybody have a solution to this problem?
Now, I used this code:
let paddingView = UIView(frame: CGRect(x: 0.0, y: 10.0, width: 5.0, height: 20.0))
firstNameTextField.leftView = paddingView
firstNameTextField.leftViewMode = .Always
to add padding on the left side of the textfield. However, I would also like some padding on the bottom as well. And there doesn't seem to be a simple solution for adding a bottom padding.