Set textbox fill using quick in xcode

How to create some space at the beginning of a text field using swift? I looked at the message

text field filling

I do not understand what the subclass does, and how to use this class to populate.

Thank you very much

+9
source share
5 answers

fooobar.com/questions/89124 / ...

This answer is very clear,

  1. If you are a subclass of UITextField you can override textRectForBounds , editingRectForBounds and placeholderRectForBounds . These methods simply allow you to add a rectangle (frame) to the label of your text box.

  2. The newBounds method creates a rectangle (frame) that will be added to the label of the text field.

  3. Finally, your indent: let padding = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5);

  4. You now have a custom UITextField that has its own indent.

  5. If you create your new subclass, for example, this class MyTextField: UITextField , you only need to change the UITextField class that you added to the IB file.

enter image description here

+14
source

In addition to klevison-matias answer, this is the code I use when I want to add an add-on to my TextField in Swift 3

 //UITextField : override textRect, editingRect class LeftPaddedTextField: UITextField { override func textRect(forBounds bounds: CGRect) -> CGRect { return CGRect(x: bounds.origin.x + 10, y: bounds.origin.y, width: bounds.width, height: bounds.height) } override func editingRect(forBounds bounds: CGRect) -> CGRect { return CGRect(x: bounds.origin.x + 10, y: bounds.origin.y, width: bounds.width, height: bounds.height) } } 

Then in my TextField I use like this:

 let emailTextField: LeftPaddedTextField = { let textField = LeftPaddedTextField() textField.placeholder = "Enter email" textField.layer.borderColor = UIColor.lightGray.cgColor textField.layer.borderWidth = 1 textField.keyboardType = .emailAddress return textField }() let passwordTextField: LeftPaddedTextField = { let textField = LeftPaddedTextField() textField.placeholder = "Enter password" textField.layer.borderColor = UIColor.lightGray.cgColor textField.layer.borderWidth = 1 textField.isSecureTextEntry = true return textField }() 
+12
source

Create a TextField output and use the following code. Let's say "nameTextField" should add an add-on.

 let paddingView = UIView(frame: CGRectMake(x: 0, y: 0, width: 30, height: 30)) nameTextField.leftView = paddingView nameTextField.leftViewMode = .always nameTextField.placeholder = "Enter Name" 

You can add image to paddingView.

+9
source

Based on @Jorge Casariego's answer, I came up with this solution.

You can set the PaddedTextField class and the required addition through the interface constructor!

 class PaddedTextField: UITextField { @IBInspectable var padding: CGFloat = 0 override func textRect(forBounds bounds: CGRect) -> CGRect { return CGRect(x: bounds.origin.x + padding, y: bounds.origin.y, width: bounds.width - padding * 2, height: bounds.height) } override func editingRect(forBounds bounds: CGRect) -> CGRect { return CGRect(x: bounds.origin.x + padding, y: bounds.origin.y, width: bounds.width - padding * 2, height: bounds.height) } } 
+1
source

full code example: make sure your TextField points to RoundedTextField through the Story board

 import UIKit class RoundedTextField: UITextField { override func awakeFromNib() { // add rounded corner self.layer.cornerRadius = 15.0 self.clipsToBounds = false super.awakeFromNib() } // For the padding from the left override func textRect(forBounds bounds: CGRect) -> CGRect { return CGRect(x: bounds.origin.x + 15.0, y: bounds.origin.y, width: bounds.width, height: bounds.height) } override func editingRect(forBounds bounds: CGRect) -> CGRect { return CGRect(x: bounds.origin.x + 15.0, y: bounds.origin.y, width: bounds.width, height: bounds.height) } } 
0
source

All Articles