【问题标题】:Multi-line user input in iOS: UITextField vs UITextViewiOS 中的多行用户输入:UITextField 与 UITextView
【发布时间】:2017-01-09 06:07:15
【问题描述】:

我需要向用户显示一个多行文本输入“框”,其高度大于UITextField 的标准高度。最好或最正确的方法应该是什么?:

  1. 使用UITextField 并在代码中更改其高度或应用特定高度约束。
  2. 使用可编辑的UITextView。这是多行但默认没有占位符,我想我应该在代码中实现该功能。

【问题讨论】:

标签: ios swift uitextfield uitextview multiline


【解决方案1】:

您不能使用UITextField 进行多行文本,您应该使用UITextView 并实现占位符逻辑。

【讨论】:

    【解决方案2】:

    UITextField 特别是只有一行。

    使用UITextView 代替多行文本。

    要在 UITextView 中实现占位符,请使用此逻辑/代码。

    首先将 UITextView 设置为包含占位符文本并将其设置为浅灰色以模仿 UITextField 占位符文本的外观。在 viewDidLoad 中或在创建文本视图时这样做。

    对于斯威夫特

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

    对于 Objective-C

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

    然后当用户开始编辑文本视图时,如果文本视图包含占位符(即如果其文本颜色为浅灰色),则清除占位符文本并将文本颜色设置为黑色以适应用户的输入。

    对于斯威夫特

    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;
    } 
    

    然后,当用户完成对文本视图的编辑并辞去第一响应者的职务时,如果文本视图为空,则通过重新添加占位符文本并将其颜色设置为浅灰色来重置其占位符。

    对于斯威夫特

    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];
        }
    }
    

    还要在视图控制器中添加UITextViewDelegate

    【讨论】:

    • 没有一个函数没有调用。当我使用 UITextViewDelegate 时,它​​工作得很好
    • 在 Swift 中,重要的是要注意委托函数不能是私有的(它会编译但不会被调用)
    • 直到我在 viewDidLoad()-Method 中添加以下代码之前,它对我不起作用:textView.delegate = self(将 textViews 委托分配给 self)
    【解决方案3】:

    使用选项二,textField的高度无法更改,并且不显示第二行...

    占位符逻辑:

    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()
        }
    }
    

    来自Text View Placeholder Swift

    【讨论】:

      【解决方案4】:

      我肯定会选择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)
      

      【讨论】:

        【解决方案5】:

        对于 Swift 3

        UITextView 本身没有占位符属性,因此您必须使用UITextViewDelegate 方法以编程方式创建和操作一个。

        (注意:将UITextViewDelegate添加到类并设置textView.delegate = self。)

        首先将UITextView 设置为包含占位符文本并将其设置为浅灰色以模仿UITextField 的占位符文本的外观。在viewDidLoad 中或在创建文本视图时这样做。

        textView.text = "Placeholder"
        textView.textColor = UIColor.lightGray
        

        然后当用户开始编辑文本视图时,如果文本视图包含占位符(即如果其文本颜色为浅灰色),则清除占位符文本并将文本颜色设置为黑色以适应用户的输入。

        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
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2012-03-25
          • 1970-01-01
          • 2013-09-24
          • 1970-01-01
          • 1970-01-01
          • 2013-10-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多