I'm trying to add top padding to a text field, but could not find anything useful, all solution were about left and right padding which I already know about. To make things clear, I want to make it like the image below.
Note: I used label for "Description" and I have put it within the text field, but label and input overlapped.
Text Field Design
Use a customTextField class
class CustomTextField: UITextField {
let padding = UIEdgeInsets(top: 15, left: 0, bottom: 0, right: 0);
override func textRect(forBounds bounds: CGRect) -> CGRect {
return UIEdgeInsetsInsetRect(bounds, padding)
}
override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
return UIEdgeInsetsInsetRect(bounds, padding)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return UIEdgeInsetsInsetRect(bounds, padding)
}
}
Swift 4 version, as per Micah's answer below:
class CustomTextField: UITextField {
let padding = UIEdgeInsets(top: 15, left: 0, bottom: 0, right: 0);
override func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: padding)
}
override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: padding)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: padding)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With