Multi-line user input in iOS: UITextField vs UITextView
Solution 1
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.
Solution 2
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.
Solution 3
For Swift 3
UITextView
doesn't inherently have a placeholder property so you'd have to create and manipulate one programmatically using UITextViewDelegate
methods.
(Note: Add UITextViewDelegate
to the class and set textView.delegate = self
.)
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.
textView.text = "Placeholder"
textView.textColor = UIColor.lightGray
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.
func textViewDidBeginEditing(_ textView: UITextView) {
if textView.textColor == UIColor.lightGray {
textView.text = nil
textView.textColor = UIColor.black
}
}
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.
func textViewDidEndEditing(_ textView: UITextView) {
if textView.text.isEmpty {
textView.text = "Placeholder"
textView.textColor = UIColor.lightGray
}
}
Solution 4
You can't do multiline text using UITextField
, You should go with UITextView
and implement the placeholder logic.
Solution 5
I would definitely go with UITextView
. In case you want to set placeholder text, UITextView+Placeholder
allows to set it easily. You can install it using
pod 'UITextView+Placeholder', '~> 1.2'
in your pod file. Now you can import header
#import <UITextView+Placeholder/UITextView+Placeholder.h>
which helps to set placeholder easily.
UITextView *textView = [[UITextView alloc] init];
textView.placeholder = @"How are you?";
textView.placeholderColor = [UIColor lightGrayColor]; // optional
textView.attributedPlaceholder = ... // NSAttributedString (optional)
Related videos on Youtube
AppsDev
Updated on July 09, 2022Comments
-
AppsDev almost 2 years
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?:- Using a
UITextField
and change its height in code or by applying a certain height constraint. - Using an editable
UITextView
. This is multi-line but it has no placeholder by default, I guess I should implement that feature in code.
-
Prashant Tukadiya over 7 yearsyou should go with option 2 , because with changing height of textfield never shows it in two line
-
Reynaldo Aguilar over 6 yearsCheck out this simple library: github.com/rlaguilar/MultilineTextField
- Using a
-
iman kazemayni about 6 yearsnone of functions didn't call. when i used UITextViewDelegate, it worked perfectly
-
nbloqs over 5 yearsIn Swift it's important to note that the delegate functions can't be private (it will compile but these will not be called)
-
Jonas Deichelmann over 4 yearsIt didn't worked for me until I added in the viewDidLoad()-Method the following code:
textView.delegate = self
(assigning the textViews delegate to self)