Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-line user input in iOS: UITextField vs UITextView

I need to show users a multi-line text input "box" with a height greater than the standard height of a UITextField. What the best or most correct approach should be?:

  1. Using a UITextField and change its height in code or by applying a certain height constraint.
  2. Using an editable UITextView. This is multi-line but it has no placeholder by default, I guess I should implement that feature in code.
like image 339
AppsDev Avatar asked Sep 01 '16 11:09

AppsDev


People also ask

What is UITextField?

An object that displays an editable text area in your interface.

What is UITextView in Swift?

Overview. UITextView supports the display of text using custom style information and also supports text editing. You typically use a text view to display multiple lines of text, such as when displaying the body of a large text document. This class supports multiple text styles through use of the attributedText property ...

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.

Use UITextView instead for multiline text.

To implement the placeholder in UITextView use this logic/code.

First set the UITextView to contain the placeholder text and set it to a light gray color to mimic the look of a UITextField's placeholder text. Either do so in the viewDidLoad or upon the text view's creation.

For Swift

textView.text = "Placeholder" textView.textColor = UIColor.lightGrayColor() 

For Objective-C

textView.text = @"Placeholder"; textView.textColor =[UIColor lightGrayColor]; 

Then when the user begins to edit the text view, if the text view contains a placeholder (i.e. if its text color is light gray) clear the placeholder text and set the text color to black in order to accommodate the user's entry.

For Swift

func textViewDidBeginEditing(textView: UITextView) {     if textView.textColor == UIColor.lightGrayColor() {         textView.text = nil         textView.textColor = UIColor.blackColor()     } } 

For Objective-C

- (BOOL) textViewShouldBeginEditing:(UITextView *)textView {     if (textView.textColor == [UIColor lightGrayColor]) {         textView.text = @"";         textView.textColor = [UIColor blackColor];     }      return YES; }  

Then when the user finishes editing the text view and it's resigned as the first responder, if the text view is empty, reset its placeholder by re-adding the placeholder text and setting its color to light gray.

For Swift

func textViewDidEndEditing(textView: UITextView) {     if textView.text.isEmpty {         textView.text = "Placeholder"         textView.textColor = UIColor.lightGrayColor()     } } 

For Objective-C

- (void)textViewDidEndEditing:(UITextView *)textView{     if ([textView.text isEqualToString:@""]) {         textView.text = @"Placeholder";         textView.textColor =[UIColor lightGrayColor];     } } 

Also do add UITextViewDelegate in the view controller.

like image 181
Deepraj Chowrasia Avatar answered Sep 30 '22 08:09

Deepraj Chowrasia


Go with option two, the height of the textField cannot be changed and it doesn't display the second line...

PLACEHOLDER LOGIC:

textView.text = "Placeholder" textView.textColor = UIColor.lightGrayColor()  func textViewDidBeginEditing(textView: UITextView) {     if textView.textColor == UIColor.lightGrayColor() {         textView.text = nil         textView.textColor = UIColor.blackColor()     } } func textViewDidEndEditing(textView: UITextView) {     if textView.text.isEmpty {         textView.text = "Placeholder"         textView.textColor = UIColor.lightGrayColor()     } } 

From Text View Placeholder Swift.

like image 37
Mr. Xcoder Avatar answered Sep 30 '22 08:09

Mr. Xcoder