Create a space at the beginning of a UITextField

I want to leave some space at the beginning of the UITextField, like here: Add lefthand margin to the UITextField

But I do not know how to do this with Swift.

+122
ios cocoa-touch uitextfield swift
Aug 18 '14 at 16:04
source share
18 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 another indent, but you can customize it. This class does not care about rightView and leftView in the text field. If you want this to be handled correctly, you can use something like (example in objc, and I only need 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; } 
+221
Nov 21 '14 at 17:13
source share

If you use the extension, there is no need to subclass UITextField, and new functionality will be available for any UITextField in your application:

 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 indent a text field anywhere in my application, I just do the following:

  textField.setLeftPaddingPoints(10) textField.setRightPaddingPoints(10) 

Using Swift extensions, functionality is added to the UITextField directly, without subclasses.

Hope this helps!

+144
Nov 16 '16 at 16:06
source share

X, Y, Z - your desired values

 textField.layer.sublayerTransform = CATransform3DMakeTranslation(x, y, z) 
+66
Feb 05 '16 at 9:28
source share

This margin can be achieved by setting leftView / rightView to a UITextField .

Updated for Swift 4

 // Create a padding view for padding on left textField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: textField.frame.height)) textField.leftViewMode = .always // Create a padding view for padding on right textField.rightView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: textField.frame.height)) textField.rightViewMode = .always 

I just added / placed a UIView in the left and right side of the text box. So, now the input will begin after viewing.

thank

Hope this helps ...

+37
Oct 19 '15 at 6:08
source share

Swift 4, Xcode 9

I like Pheepster's answer , but what about the fact that we are doing all this from an extension without requiring VC code or any subclass:

 import UIKit @IBDesignable extension UITextField { @IBInspectable var paddingLeftCustom: CGFloat { get { return leftView!.frame.size.width } set { let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: newValue, height: frame.size.height)) leftView = paddingView leftViewMode = .always } } @IBInspectable var paddingRightCustom: CGFloat { get { return rightView!.frame.size.width } set { let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: newValue, height: frame.size.height)) rightView = paddingView rightViewMode = .always } } } 
+28
Nov 29 '17 at 11:59 on
source share

Simple quick fix 3 - add code to view DidLoad:

 let indentView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 20)) textField.leftView = indentView textField.leftViewMode = .always 

No need for a ridiculous long code

+15
Feb 10 '17 at 20:01
source share

in Swift 4.2 and Xcode 10

Initially, my text box looks like this.

enter image description here

After adding the indentation on the left side of my text box ...

enter image description here

 //Code for left padding textFieldName.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: textFieldName.frame.height)) textFieldName.leftViewMode = .always 

Thus, we can also create the right side. (TextFieldName.rightViewMode = .always)

If you need code like SharedInstance (write once, use each program), see the code below.

 //This is my shared class import UIKit class SharedClass: NSObject { static let sharedInstance = SharedClass() //This is my padding function. func textFieldLeftPadding(textFieldName: UITextField) { // Create a padding view textFieldName.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 3, height: textFieldName.frame.height)) textFieldName.leftViewMode = .always//For left side padding textFieldName.rightViewMode = .always//For right side padding } private override init() { } } 

Now call this function as follows.

 //This single line is enough SharedClass.sharedInstance.textFieldLeftPadding(textFieldName:yourTF) 
+14
Jul 18 '18 at 13:25
source share

To create an indentation view for a UITextField in Swift 5

 func txtPaddingVw(txt:UITextField) { let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: 5)) txt.leftViewMode = .always txt.leftView = paddingView } 
+9
Mar 21 '17 at 11:33
source share

A subclass of UITextField is the way to go. Open the playground and add the following code:

 class MyTextField : UITextField { var leftTextMargin : CGFloat = 0.0 override func textRectForBounds(bounds: CGRect) -> CGRect { var newBounds = bounds newBounds.origin.x += leftTextMargin return newBounds } override func editingRectForBounds(bounds: CGRect) -> CGRect { var newBounds = bounds newBounds.origin.x += leftTextMargin return newBounds } } let tf = MyTextField(frame: CGRect(x: 0, y: 0, width: 100, height: 44)) tf.text = "HELLO" tf.leftTextMargin = 25 tf.setNeedsLayout() tf.layoutIfNeeded() 
+7
Aug 18 '14 at 20:29
source share

Create a UIView with the required fill space and add it to the textfield.leftView element and set the textfield.leftViewMode member to UITextFieldViewMode.Always

 // For example if you have textfield named title @IBOutlet weak var title: UITextField! // Create UIView let paddingView : UIView = UIView(frame: CGRectMake(0, 0, 5, 20)) //Change your required space instaed of 5. title.leftView = paddingView title.leftViewMode = UITextFieldViewMode.Always 
+6
Apr 21 '16 at 14:36
source share

Here is the Haagenti answer updated to Swift 4.2:

 class PaddedTextField: UITextField { func getPadding(plusExtraFor clearButtonMode: ViewMode) -> UIEdgeInsets { var padding = UIEdgeInsets(top: 11, left: 16, bottom: 11, right: 16) // Add additional padding on the right side when showing the clear button if self.clearButtonMode == .always || self.clearButtonMode == clearButtonMode { padding.right = 28 } return padding } override open func textRect(forBounds bounds: CGRect) -> CGRect { let padding = getPadding(plusExtraFor: .unlessEditing) return bounds.inset(by: padding) } override open func placeholderRect(forBounds bounds: CGRect) -> CGRect { let padding = getPadding(plusExtraFor: .unlessEditing) return bounds.inset(by: padding) } override open func editingRect(forBounds bounds: CGRect) -> CGRect { let padding = getPadding(plusExtraFor: .whileEditing) return bounds.inset(by: padding) } } 

Link: Upgrade to Swift 4.2 .

Edit : The account for the clear button.

+6
Oct 28 '18 at 13:57
source share

Put this code in your viewDidLoad() :

 textField.delegate = self let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: self.textField.frame.height)) textField.leftView = paddingView textField.leftViewMode = UITextFieldViewMode.always 

This works for me :)

+5
Oct 13 '17 at 13:06 on
source share

ScareCrow answer in Swift 3

 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) } 
+4
Dec 22 '16 at 10:35
source share

In Swift 3. You can use a custom UITextField with an indent that is set in its constructor. Not required for additional declaration in the controller.

 class CustomTextField : UITextField { private let indentView = UIView(frame: CGRect(x: 0, y:0, width: 10, height: 10)) required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.leftView = indentView self.leftViewMode = .always } } 
+4
Mar 13 '17 at 13:32
source share

Easy way: do it by extending UITextField

 extension UITextField { func setPadding(left: CGFloat? = nil, right: CGFloat? = nil){ if let left = left { let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: left, height: self.frame.size.height)) self.leftView = paddingView self.leftViewMode = .always } if let right = right { let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: right, height: self.frame.size.height)) self.rightView = paddingView self.rightViewMode = .always } } } 

Then you can install the complement to any edge this way:

 textField.setPadding(left: 5, right: 5) 
+3
May 16 '18 at 20:04
source share

I prefer to use IBDesignable and IBInspectable to allow me to set IBInspectable through Xcode storyboards and save it again. I also updated the code to work in Swift 4.

 import Foundation import UIKit @IBDesignable class PaddableTextField: UITextField { var padding = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0) @IBInspectable var left: CGFloat = 0 { didSet { adjustPadding() } } @IBInspectable var right: CGFloat = 0 { didSet { adjustPadding() } } @IBInspectable var top: CGFloat = 0 { didSet { adjustPadding() } } @IBInspectable var bottom: CGFloat = 0 { didSet { adjustPadding() } } func adjustPadding() { padding = UIEdgeInsets(top: top, left: left, bottom: bottom, right: right) } override func prepareForInterfaceBuilder() { super.prepareForInterfaceBuilder() } override func textRect(forBounds bounds: CGRect) -> CGRect { return bounds.inset(by: UIEdgeInsets(top: top, left: left, bottom: bottom, right: right)) } override func placeholderRect(forBounds bounds: CGRect) -> CGRect { return bounds.inset(by: UIEdgeInsets(top: top, left: left, bottom: bottom, right: right)) } override func editingRect(forBounds bounds: CGRect) -> CGRect { return bounds.inset(by: UIEdgeInsets(top: top, left: left, bottom: bottom, right: right)) } } 
+1
Dec 02 '18 at 3:43
source share

* UITextField extension in Swift 5 *

 import UIKit @IBDesignable extension UITextField { @IBInspectable var paddingLeftCustom: CGFloat { get { return leftView!.frame.size.width } set { let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: newValue, height: frame.size.height)) leftView = paddingView leftViewMode = .always } } @IBInspectable var paddingRightCustom: CGFloat { get { return rightView!.frame.size.width } set { let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: newValue, height: frame.size.height)) rightView = paddingView rightViewMode = .always } } } 
+1
May 26 '19 at 23:31
source share

This line of code saved me:

For Xamarin.iOS:

 textField.Layer.SublayerTransform = CATransform3D.MakeTranslation(5, 0, 0); 

For Swift:

 textField.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0); 
0
Jun 25 '19 at 13:13
source share



All Articles