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.

3
  • 1
    UITextField doesn't support multiple lines. You need to use UITextView and handle the character limits in textViewShouldChangeCharacters. Commented Oct 22, 2018 at 10:34
  • 2
    Possible duplicate of How to create a multiline UITextfield? Commented Oct 22, 2018 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. Commented Oct 22, 2018 at 13:30

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

4 Comments

How would a textView accept user input like a textfield does
set isEditable to true
@dirkgroten thanks for responding, I was about to respond.
Thanks, dirkgroten and Sandy it solved the issue instead I have used MultilineTextField library with classes and support files as I was unable to impart action to the textView.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.