Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create space at the beginning of a UITextField

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.

like image 958
LinusGeffarth Avatar asked Aug 18 '14 16:08

LinusGeffarth


2 Answers

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; } 
like image 192
Haagenti Avatar answered Sep 19 '22 17:09

Haagenti


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!

like image 22
Pheepster Avatar answered Sep 20 '22 17:09

Pheepster