Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create textfield padding in Swift 4?

I have a textfield called

nameTextField

I rounded the corners with the

nameTexfield.layer.cornerRadius = 5

Now, the text within the textfield is touching the left side. I want to create padding between the text and border. Every post I find uses cgRect, but Swift no longer supports that. Please provide the correct code if you can figure it out and please explain the answer if you can. I appreciate the help! I also need to know where to put the code if there is any.

like image 811
Levi K Avatar asked Nov 30 '17 20:11

Levi K


People also ask

How do I add padding to TextField?

A JTextField class will generate an ActionListener interface when we trying to enter some input inside it. 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 make a TextField editable in Swift?

Set the Boolean variable to false, which enables editing in the text field. When someone taps the Done button, isEditing becomes false. Set the Boolean variable to true, which disables editing in the text field.

What is Leftviewmode in Swift?

The overlay view that displays on the left (or leading) side of the text field.

How do I change the height of a TextField in Swift?

Click the attribute inspector for the textField and then change the Border style to second one and you will be able to change the height.


1 Answers

Better to subclass the textview than to add a left view incase you are planning to add an image as a left view.

class UITextFieldPadding: UITextField {

    let padding = UIEdgeInsets(top: 0, left: 40, bottom: 0, right: 0)

    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)
    }
}
like image 192
Marwan Roushdy Avatar answered Oct 06 '22 01:10

Marwan Roushdy