Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set the padding of a text field using swift in xcode

Tags:

ios

swift

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

padding of text field

I don't understand what the subclass is doing and how do I use that class to padding.

Thank you very much

like image 764
sekaisan Avatar asked Aug 04 '15 16:08

sekaisan


People also ask

How do I add padding to TextField?

The important methods of a JTextField class are setText(), getText(), setBorder(), setEnabled(), etc. We can add padding to a JTextField using the setMargin(Insets s) of JTextComponent class.

How do I add padding to labels in Xcode?

You should use a container view and place the label inside of that. Then add constraints for the label, which will be equivalent to a padding. Select the label and click the constraints panel at the bottom right in the Storyboard file.


3 Answers

https://stackoverflow.com/a/27066764/846780

This answer is very clear,

  1. If you subclass UITextField you can override textRectForBounds, editingRectForBounds and placeholderRectForBounds. These methods just allow you to add a rect (frame) to your textfield's label.

  2. newBounds method create the rect (frame) that will be added to textfield's label.

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

  4. Now you have a custom UITextField that has a custom padding.

  5. If you create your new subClass like this class MyTextField: UITextField for example, you only need to change the class of the UITextField that you've added into IB file.

enter image description here

like image 94
Klevison Avatar answered Oct 18 '22 05:10

Klevison


Complementing the answer of klevison-matias this is the code I use when I want to add a padding 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 in this way:

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
}()
like image 15
Jorge Casariego Avatar answered Oct 18 '22 03:10

Jorge Casariego


Create TextField outlet and use following code. Say "nameTextField" has to add padding.

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 in paddingView.

like image 11
Avijit Nagare Avatar answered Oct 18 '22 05:10

Avijit Nagare