文本视图占位符Swift [英] Text View Placeholder Swift

查看:94
本文介绍了文本视图占位符Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个使用文本视图的应用程序。现在我希望文本视图的占位符类似于您可以为文本字段设置的占位符。你会如何使用swift实现这一目标。

I'm making an application which uses a Text View. Now I want the Text View to have a placeholder similar to the one you can set for a Text Field. How would you accomplish this using swift.

有谁知道怎么做?

推荐答案

针对Swift 4更新

UITextView 本身并不具备有一个占位符属性,所以你必须使用 UITextViewDelegate 方法以编程方式创建和操作一个。我建议使用下面的解决方案#1或#2,具体取决于所需的行为。

UITextView doesn't inherently have a placeholder property so you'd have to create and manipulate one programmatically using UITextViewDelegate methods. I recommend using either solution #1 or #2 below depending on the desired behavior.

注意:对于任一解决方案,添加 UITextViewDelegate 到该类并设置 textView.delegate = self 以使用文本视图的委托方法。

Note: For either solution, add UITextViewDelegate to the class and set textView.delegate = self to use the text view’s delegate methods.

解决方案#1 - 如果您希望占位符在用户选择文本视图后立即消失:

Solution #1 - If you want the placeholder to disappear as soon as the user selects the text view:

首先设置 UITextView 以包含占位符文本并将其设置为浅灰色以模仿看一下 UITextField 的占位符文本。可以在 viewDidLoad 中或在文本视图创建时执行此操作。

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






解决方案#2 - 如果您希望占位符在文本视图为空时显示,即使选择了文本视图:


Solution #2 - If you want the placeholder to show whenever the text view is empty, even if the text view’s selected:

首先在<$ c $中设置占位符c> viewDidLoad :

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

textView.becomeFirstResponder()

textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument)

(注意:由于OP想要在视图加载后选择文本视图,因此我将文本视图选择合并到上面的代码。如果这不是您想要的行为,并且您不希望在查看加载时选择文本视图,请从上面的代码块中删除最后两行。)

(Note: Since the OP wanted to have the text view selected as soon as the view loads, I incorporated text view selection into the above code. If this is not your desired behavior and you do not want the text view selected upon view load, remove the last two lines from the above code chunk.)

然后使用 shouldChangeTextInRange UITextViewDelegate 方法,如下所示:

Then utilize the shouldChangeTextInRange UITextViewDelegate method, like so:

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {

    // Combine the textView text and the replacement text to
    // create the updated text string
    let currentText:String = textView.text
    let updatedText = (currentText as NSString).replacingCharacters(in: range, with: text)

    // If updated text view will be empty, add the placeholder
    // and set the cursor to the beginning of the text view
    if updatedText.isEmpty {

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

        textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument)
    }

    // Else if the text view's placeholder is showing and the
    // length of the replacement string is greater than 0, set 
    // the text color to black then set its text to the
    // replacement string
     else if textView.textColor == UIColor.lightGray && !text.isEmpty {
        textView.textColor = UIColor.black
        textView.text = text
    }

    // For every other case, the text should change with the usual
    // behavior...
    else {
        return true
    }

    // ...otherwise return false since the updates have already
    // been made
    return false
}

还有实现 textViewDidChangeSelection 以防止用户在占位符可见时更改光标的位置。 (注意: textViewDidChangeSelection 在视图加载之前被调用,因此只有在窗口可见时才检查文本视图的颜色):

And also implement textViewDidChangeSelection to prevent the user from changing the position of the cursor while the placeholder's visible. (Note: textViewDidChangeSelection is called before the view loads so only check the text view's color if the window is visible):

func textViewDidChangeSelection(_ textView: UITextView) {
    if self.view.window != nil {
        if textView.textColor == UIColor.lightGray {
            textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument)
        }
    }
}

这篇关于文本视图占位符Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆