iOS 中的多行用户输入:UITextField vs UITextView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39270123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multi-line user input in iOS: UITextField vs UITextView
提问by AppsDev
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?:
我需要向用户显示一个多行文本输入“框”,其高度大于UITextField
. 最好或最正确的方法应该是什么?:
- 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.
- 使用 a
UITextField
并在代码中更改其高度或通过应用特定的高度约束。 - 使用可编辑的
UITextView
. 这是多行但默认情况下没有占位符,我想我应该在代码中实现该功能。
回答by Deepraj Chowrasia
UITextField
is specifically one line only.
UITextField
特别是只有一行。
Use UITextView
instead for multiline text.
使用UITextView
而不是为多行文本。
To implement the placeholder in UITextView use this logic/code.
要在 UITextView 中实现占位符,请使用此逻辑/代码。
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.
首先将 UITextView 设置为包含占位符文本并将其设置为浅灰色以模仿 UITextField 占位符文本的外观。无论是在这样做viewDidLoad
或者在文本视图的创建。
For Swift
对于斯威夫特
textView.text = "Placeholder"
textView.textColor = UIColor.lightGrayColor()
For Objective-C
对于 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
对于 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
对于 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.
还要UITextViewDelegate
在视图控制器中添加。
回答by Mr. Xcoder
Go with option two, the height of the textField cannot be changed and it doesn't display the second line...
使用选项二,textField 的高度不能改变,它不显示第二行......
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()
}
}
回答by Naveen Kumar M
For Swift 3
对于 Swift 3
UITextView
doesn't inherently have a placeholder property so you'd have to create and manipulate one programmatically using UITextViewDelegate
methods.
UITextView
本身没有占位符属性,因此您必须使用UITextViewDelegate
方法以编程方式创建和操作一个。
(Note: Add UITextViewDelegate
to the class and set textView.delegate = self
.)
(注意:添加UITextViewDelegate
到类并设置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.
首先将 设置UITextView
为包含占位符文本并将其设置为浅灰色以模仿 aUITextField
的占位符文本的外观。无论是在这样做viewDidLoad
或者在文本视图的创建。
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
}
}
回答by Sukhpreet
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
我肯定会去的UITextView
。如果您想设置占位符文本,UITextView+Placeholder
可以轻松设置。您可以使用安装它
pod 'UITextView+Placeholder', '~> 1.2'
in your pod file. Now you can import header
在你的 pod 文件中。现在您可以导入标题
#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)
回答by venky
You can't do multiline text using UITextField
, You should go with UITextView
and implement the placeholder logic.
您不能使用 来处理多行文本UITextField
,您应该使用UITextView
并实现占位符逻辑。