4

I am trying to make a form and the requirement is to have a UItextfield with multiple lines which means, when a specific number of characters are entered in one line the blip moves to next line also on hitting enter it should enter the next line as well.

Currently, I am working with Xcode 9.4 but it offers a single line text field.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Mayank Wadhwa
  • 43
  • 1
  • 1
  • 8
  • 1
    `UITextField` doesn't support multiple lines. You need to use `UITextView` and handle the character limits in `textViewShouldChangeCharacters`. – Rakesha Shastri Oct 22 '18 at 10:34
  • 2
    Possible duplicate of [How to create a multiline UITextfield?](https://stackoverflow.com/questions/1345561/how-to-create-a-multiline-uitextfield) – ielyamani Oct 22 '18 at 11:25
  • you have to use UITextView. If you wanna have textfield like border you can use a UITextField behind transparent UITextView and set height constraints to match UITextView. You can use UITextViewDelegate for your text limitations. – RJE Oct 22 '18 at 13:30

2 Answers2

5

I used UITextView with this code:

lazy var commentTextView: UITextView = {
    // Create a TextView.
    let textView: UITextView = UITextView()

    // Round the corners.
    textView.layer.masksToBounds = true

    // Set the size of the roundness.
    textView.layer.cornerRadius = 20.0

    // Set the thickness of the border.
    textView.layer.borderWidth = 1

    // Set the border color to black.
    textView.layer.borderColor = UIColor.systemGray.cgColor

    // Set the font.
    textView.font = UIFont.systemFont(ofSize: 16.0)

    // Set font color.
    textView.textColor = UIColor.black

    // Set left justified.
    textView.textAlignment = NSTextAlignment.left

    // Automatically detect links, dates, etc. and convert them to links.
    textView.dataDetectorTypes = UIDataDetectorTypes.all

    // Set shadow darkness.
    textView.layer.shadowOpacity = 0.5

    // Make text uneditable.
    textView.isEditable = true

    return textView
}()

and this is the result

enter image description here

Jorge Casariego
  • 21,948
  • 6
  • 90
  • 97
2

You should use UITextView for multiline string or there is third party library which you can use. MultilineTextField

Sanoj Kashyap
  • 5,020
  • 4
  • 49
  • 75