0

Below is an image of some UITextFields. Regarding the large one on the bottom, how do I get it to start text at the top left (not the middle), and how do I get them all to start a bit to the right. As you can see, they are awkwardly close to the left edge.


enter image description here

Gabe Spound
  • 146
  • 2
  • 14

4 Answers4

2

For textField, override following methods:

class InsetTextField: UITextField {

    var inset: CGFloat = 10

    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.insetBy(dx: inset, dy: 0)
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.insetBy(dx: inset, dy: 0)
    }

    override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.insetBy(dx: inset, dy: 0)
    }

}

For textView:

textView.contentInset = UIEdgeInsetsMake(inset, inset, inset, inset);

And you can be involved when the textView is done editing, check out this question.

Community
  • 1
  • 1
AlvinZhu
  • 448
  • 6
  • 13
1

You can sublcass to a custom TextField:

import UIKit

class CustomTextField: UITextField {

    var inset:CGFloat = 12  // You can set the inset you want

    override func textRect(forBounds bounds: CGRect) -> CGRect {

        return bounds.insetBy(dx: inset, dy: 0)
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {

        return bounds.insetBy(dx: inset, dy: 0)
    }

}

The result, you can see the inset of the CustomTextField:


Edit

enter image description here

aircraft
  • 25,146
  • 28
  • 91
  • 166
1

By default, UITextFields only have one line. Based off your image, I'd assume you want the user to have space to type a paragraph. For paragraphs, it is better to use UITextViews. It'd be a simpler solution for your problem.

I always use TextViews for biographies because it just makes my life much easier.

Subject X
  • 43
  • 4
  • I can't use a textview because I need to be able to create Actions when they are done editing, and they don't have that option. – Gabe Spound Feb 19 '17 at 06:05
1

add left padding of your textfield

let paddingVie = UIView(frame: CGRect(x: 0, y:0, width: 10, height: 10))
yourtextField.leftView = paddingVie
yourtextField.leftViewMode = .always

for textview add

yourTextVieName.textContainerInset =
   UIEdgeInsetsMake(8,5,8,5); // top, left, bottom, right
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143