By raaz


2009-08-28 07:37:46 8 Comments

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.

9 comments

@Dave G 2016-05-12 12:36:37

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

@McFlys 2016-12-27 16:35:36

There is another option that worked for me:

Subclass UITextField and overwrite:

- (void)drawTextInRect:(CGRect)rect

In this method you can for example:

    NSDictionary *attributes = @{ NSFontAttributeName : self.font,
                                  NSForegroundColorAttributeName : self.textColor };

    [self.text drawInRect:verticalAlignedRect withAttributes:attributes];

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:

self.defaultTextAttributes

which will force one line text rendering

@Monir Khlaf 2016-01-23 20:39:31

use UITextView instead of UITextField

@h4xxr 2009-08-28 08:34:18

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.

@Gennadiy Ryabkin 2014-12-09 11:24:54

So tell me please how native Calendar app makes View with placeholder and word wrap for notes at event adding screen ?

@Mani 2014-12-28 18:07:33

how to set placeholder for UITextView?

@Andrew Romanov 2015-03-04 11:20:52

You can't add placeholder to TextView, but you can add a gray label over TextView, and hide it if user input some text.

@Markus 2016-01-21 23:09:10

Please can anybody help me with the following question? If the user writes bulleted text (multiple line breaks) in UITextView, how to detect the total number of line breaks?

@Nathan F. 2019-10-15 21:53:44

@Markus you should open a new question for that

@Tony 2014-02-02 22:43:57

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?

@nevan king 2009-08-28 08:32:29

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

@Rudi 2012-11-20 15:27:04

Ok I did it with some trick ;) First build a UITextField and increased it's size like this :

CGRect frameRect = textField.frame;
        frameRect.size.height = 53;
        textField.frame = frameRect;

Then build a UITextView exactly in the same area that u made my UITextField, and deleted its background color. Now it looks like that u have a multiple lines TextField !

@phatmann 2013-12-21 17:02:53

I used auto layout and pinned all the sides of the UITextField to the UITextView. Really great trick, thanks!

@Esteve 2014-03-07 13:44:38

please @phantmann , can you show us your code for auto layout?

@Nate Symer 2015-08-10 19:16:39

This creates a UITextView. The point of having a UITextField is that it behaves differently than a UITextView.

@Sam Ballantyne 2016-11-19 18:14:53

There are a bunch of differences between UITextView and UITextfield: they inherit from UIScrollView vs UIControl, not-editable-by-default vs. editable-by-default, multiline vs. single-line, different delegate protocols, no-placeholder vs. placeholder.

@Sam Ballantyne 2016-11-19 18:16:44

UITextView is for paragraphs of text. Use it if you need a paragraph of text, and not otherwise.

@DiscDev 2013-05-02 16:31:58

If you must have a UITextField with 2 lines of text, one option is to add a UILabel as a subview of the UITextField for the second line of text. I have a UITextField in my app that users often do not realize is editable by tapping, and I wanted to add some small subtitle text that says "Tap to Edit" to the UITextField.

CGFloat tapLlblHeight = 10;
UILabel *tapEditLbl = [[UILabel alloc] initWithFrame:CGRectMake(20, textField.frame.size.height - tapLlblHeight - 2, 70, tapLlblHeight)];
tapEditLbl.backgroundColor = [UIColor clearColor];
tapEditLbl.textColor = [UIColor whiteColor];
tapEditLbl.text = @"Tap to Edit";

[textField addSubview:tapEditLbl];

@MonsieurDart 2010-11-10 14:39:45

Besides from the multiple line behaviour, the main difference between UITextView and UITextField is that the UITextView does not propose a placeholder. To bypass this limitation, you can use a UITextView with a "fake placeholder."

See this SO question for details: Placeholder in UITextView.

Related Questions

Sponsored Content

94 Answered Questions

44 Answered Questions

[SOLVED] Set the maximum character length of a UITextField

32 Answered Questions

[SOLVED] iPhone UITextField - Change placeholder text color

  • 2009-08-27 10:36:19
  • adam
  • 339637 View
  • 554 Score
  • 32 Answer
  • Tags:   ios uitextfield

27 Answered Questions

[SOLVED] Text inset for UITextField?

32 Answered Questions

[SOLVED] Set padding for UITextField with UITextBorderStyleNone

35 Answered Questions

[SOLVED] iOS - Dismiss keyboard when touching outside of UITextField

2 Answered Questions

[SOLVED] How to create a multi-line UITextField?

  • 2012-01-26 19:09:16
  • user836026
  • 4563 View
  • 0 Score
  • 2 Answer
  • Tags:   ios

3 Answered Questions

[SOLVED] iPhone - UITextView should look like a UITextField

Sponsored Content