I want to leave a bit of space at the beginning of a UITextField, just like here: Add lefthand margin to UITextField
But I don't know how to do that with Swift.
This is what I am using right now:
Swift 4.2
class TextField: UITextField { let padding = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5) override open func textRect(forBounds bounds: CGRect) -> CGRect { return bounds.inset(by: padding) } override open func placeholderRect(forBounds bounds: CGRect) -> CGRect { return bounds.inset(by: padding) } override open func editingRect(forBounds bounds: CGRect) -> CGRect { return bounds.inset(by: padding) } }
Swift 4
class TextField: UITextField { let padding = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5) override open func textRect(forBounds bounds: CGRect) -> CGRect { return UIEdgeInsetsInsetRect(bounds, padding) } override open func placeholderRect(forBounds bounds: CGRect) -> CGRect { return UIEdgeInsetsInsetRect(bounds, padding) } override open func editingRect(forBounds bounds: CGRect) -> CGRect { return UIEdgeInsetsInsetRect(bounds, padding) } }
Swift 3:
class TextField: UITextField { let padding = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5) 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) } }
I never set a other padding but you can tweak. This class doesn't take care of the rightView and leftView on the textfield. If you want that to be handle correctly you can use something like (example in objc and I only needed the rightView:
- (CGRect)textRectForBounds:(CGRect)bounds { CGRect paddedRect = UIEdgeInsetsInsetRect(bounds, self.insets); if (self.rightViewMode == UITextFieldViewModeAlways || self.rightViewMode == UITextFieldViewModeUnlessEditing) { return [self adjustRectWithWidthRightView:paddedRect]; } return paddedRect; } - (CGRect)placeholderRectForBounds:(CGRect)bounds { CGRect paddedRect = UIEdgeInsetsInsetRect(bounds, self.insets); if (self.rightViewMode == UITextFieldViewModeAlways || self.rightViewMode == UITextFieldViewModeUnlessEditing) { return [self adjustRectWithWidthRightView:paddedRect]; } return paddedRect; } - (CGRect)editingRectForBounds:(CGRect)bounds { CGRect paddedRect = UIEdgeInsetsInsetRect(bounds, self.insets); if (self.rightViewMode == UITextFieldViewModeAlways || self.rightViewMode == UITextFieldViewModeWhileEditing) { return [self adjustRectWithWidthRightView:paddedRect]; } return paddedRect; } - (CGRect)adjustRectWithWidthRightView:(CGRect)bounds { CGRect paddedRect = bounds; paddedRect.size.width -= CGRectGetWidth(self.rightView.frame); return paddedRect; }
If you use an extension, there is no need to subclass UITextField and the new functionality will be made available to any UITextField in your app:
extension UITextField { func setLeftPaddingPoints(_ amount:CGFloat){ let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height)) self.leftView = paddingView self.leftViewMode = .always } func setRightPaddingPoints(_ amount:CGFloat) { let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height)) self.rightView = paddingView self.rightViewMode = .always } }
When I need to set the padding of a text field anywhere in my application, I simply do the following:
textField.setLeftPaddingPoints(10) textField.setRightPaddingPoints(10)
Using Swift extensions, the functionality is added to the UITextField directly without subclassing.
Hope this helps!
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