iOS中的多行用户input:UITextField与UITextView
我需要向用户显示一个高度大于UITextField
标准高度的多行文本input“框”。 最好或最正确的方法是什么?
- 使用
UITextField
并在代码中改变其高度或通过应用一定的高度约束。 - 使用可编辑的
UITextView
。 这是多行的,但它没有默认的占位符,我想我应该在代码中实现该function。
UITextField
只是一行。
使用UITextView
而不是多行文本。
要在UITextView中实现占位符,使用这个逻辑/代码。
首先设置UITextView以包含占位符文本,并将其设置为浅灰色以模仿UITextField的占位符文本的外观。 要么在viewDidLoad
要么在文本视图的创build。
对于Swift
textView.text = "Placeholder" textView.textColor = UIColor.lightGrayColor()
对于Objective-C
textView.text = @"Placeholder"; textView.textColor =[UIColor lightGrayColor];
然后,当用户开始编辑文本视图时,如果文本视图包含占位符(即其文本颜色为浅灰色),请清除占位符文本,并将文本颜色设置为黑色以适应用户的input。
对于Swift
func textViewDidBeginEditing(textView: UITextView) { if textView.textColor == UIColor.lightGrayColor() { textView.text = nil textView.textColor = UIColor.blackColor() } }
对于Objective-C
- (BOOL) textViewShouldBeginEditing:(UITextView *)textView { if (textView.textColor == [UIColor lightGrayColor]) { textView.text = @""; textView.textColor = [UIColor blackColor]; } return YES; }
然后,当用户完成文本视图的编辑并将其作为第一响应者辞职时,如果文本视图为空,则通过重新添加占位符文本并将其颜色设置为浅灰色来重置其占位符。
对于Swift
func textViewDidEndEditing(textView: UITextView) { if textView.text.isEmpty { textView.text = "Placeholder" textView.textColor = UIColor.lightGrayColor() } }
对于Objective-C
- (void)textViewDidEndEditing:(UITextView *)textView{ if ([textView.text isEqualToString:@""]) { textView.text = @"Placeholder"; textView.textColor =[UIColor lightGrayColor]; } }
去选项二,textField的高度不能改变,它不会显示第二行…
PLACEHOLDER逻辑:
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() } }
从文本视图占位符Swift 。
你不能使用UITextField
做多行文本,你应该使用UITextView
并实现占位符逻辑。
我一定会用UITextView
。 如果你想设置占位符文本, UITextView+Placeholder
允许设置它很容易。 您可以使用安装它
pod 'UITextView+Placeholder', '~> 1.2'
在您的pod文件中。 现在你可以导入标题
#import <UITextView+Placeholder/UITextView+Placeholder.h>
这有助于轻松设置占位符。
UITextView *textView = [[UITextView alloc] init]; textView.placeholder = @"How are you?"; textView.placeholderColor = [UIColor lightGrayColor]; // optional textView.attributedPlaceholder = ... // NSAttributedString (optional)
对于Swift 3
UITextView
本身并不具有占位符属性,因此您必须使用UITextViewDelegate
方法以编程方式创build和操作一个占位符属性。
(注意:将UITextViewDelegate
添加到该类并设置textView.delegate = self
。)
首先设置UITextView
以包含占位符文本,并将其设置为浅灰色以模仿UITextField
占位符文本的外观。 要么在viewDidLoad
要么在文本视图的创build。
textView.text = "Placeholder" textView.textColor = UIColor.lightGray
然后,当用户开始编辑文本视图时,如果文本视图包含占位符(即其文本颜色为浅灰色),请清除占位符文本,并将文本颜色设置为黑色以适应用户的input。
func textViewDidBeginEditing(_ textView: UITextView) { if textView.textColor == UIColor.lightGray { textView.text = nil textView.textColor = UIColor.black } }
然后,当用户完成文本视图的编辑并将其作为第一响应者辞职时,如果文本视图为空,则通过重新添加占位符文本并将其颜色设置为浅灰色来重置其占位符。
func textViewDidEndEditing(_ textView: UITextView) { if textView.text.isEmpty { textView.text = "Placeholder" textView.textColor = UIColor.lightGray } }