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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With