Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a multiline UITextfield?

I am developing an application where user has to write some information. For this purpose I need a UITextField which is multi-line (in general UITextField is a single line).

As I'm Googling I find a answer of using UITextView instead of UITextfield for this purpose.

like image 392
raaz Avatar asked Aug 28 '09 07:08

raaz


People also ask

How do I create a multiline TextField in SwiftUI?

To make a multiline text field that grows with the content, we specify axis: . vertical as an initializer argument. This will make a text field grow dynamically with the content as long as there is enough space.

How do I make text field bigger in Xcode?

Step 1 : Click the Attribute Inspector, Select the Border Styles which is not the rounded one. Step 2 : Now go to Size Inspector and change the size of the TextField.


2 Answers

UITextField is specifically one-line only.

Your Google search is correct, you need to use UITextView instead of UITextField for display and editing of multiline text.

In Interface Builder, add a UITextView where you want it and select the "editable" box. It will be multiline by default.

like image 107
h4xxr Avatar answered Oct 04 '22 03:10

h4xxr


You can fake a UITextField using UITextView. The problem you'll have is that you lose the place holder functionality.

If you choose to use a UITextView and need the placeholder, do this:

In your viewDidLoad set the color and text to placeholders:

myTxtView.textColor = .lightGray myTxtView.text = "Type your thoughts here..." 

Then make the placeholder disappear when your UITextView is selected:

func textViewDidBeginEditing (textView: UITextView) {     if myTxtView.textColor.textColor == ph_TextColor && myTxtView.isFirstResponder() {         myTxtView.text = nil         myTxtView.textColor = .white     } } 

When the user finishes editing, ensure there's a value. If there isn't, add the placeholder again:

func textViewDidEndEditing (textView: UITextView) {     if myTxtView.text.isEmpty || myTxtView.text == "" {         myTxtView.textColor = .lightGray         myTxtView.text = "Type your thoughts here..."     } } 

Other features you might need to fake:

UITextField's often capitalize every letter, you can add that feature to UITableView:

myTxtView.autocapitalizationType = .words 

UITextField's don't usually scroll:

myTxtView.scrollEnabled = false 
like image 35
Dave G Avatar answered Oct 04 '22 03:10

Dave G