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.
There is another option that worked for me:
Subclass UITextField and overwrite:
In this method you can for example:
This code will render the text using as many lines as required if the rect has enough space. You could specify any other attribute depending on your needs.
Do not use:
which will force one line text rendering
Yes, a UITextView is what you're looking for. You'll have to deal with some things differently (like the return key) but you can add text to it, and it will allow you to scroll up and down if there's too much text inside.
This link has info about making a screen to enter data:
create a data entry screen
UITextField
is specifically one-line only.Your Google search is correct, you need to use
UITextView
instead ofUITextField
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.A supplement to h4xxr's answer in the above, an easier way to adjust the height of the UITextField is to select square border style in the attribute inspectors->Text Field. (By default, the border style of a UITextfield is ellipse.)
Reference: Answered Brian in here : How to set UITextField height?
Ok I did it with some trick ;) First build a
UITextField
and increased it'ssize
like this :Then build a
UITextView
exactly in the same area that u made myUITextField
, and deleted itsbackground color
. Now it looks like that u have a multiple linesTextField
!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:
Then make the placeholder disappear when your UITextView is selected:
When the user finishes editing, ensure there's a value. If there isn't, add the placeholder again:
Other features you might need to fake:
UITextField's often capitalize every letter, you can add that feature to UITableView:
UITextField's don't usually scroll: