如何在一个视图控制器中快速将占位符设置为多个文本视图

时间:2019-07-08 18:08:24

标签: ios swift uitextview placeholder

我有一个视图控制器,其中包含两个文本视图(storyTitle和storyDe​​scription)。我已经在两个文本视图中添加了占位符,下面的代码将向您展示我是如何做到的:

// Setup the story title text field
func setupStoryTitleTextView() {

    storyTitle.textColor = .gray
    storyTitle.text = "Add story title"
    storyTitle.textContainerInset = UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20)
    self.storyTitle.delegate = self

    view.addSubview(storyTitle)

}
// Setup the storyDescription text view
func setupStoryDescriptionTextView() {

    storyDescription.text = "Description of the story"
    storyDescription.textColor = .gray
    storyDescription.textContainerInset = UIEdgeInsets(top: 10, left: 20, bottom: 300, right: 20)
    self.storyDescription.delegate = self

    view.addSubview(storyDescription)

}
// To remove placeholder text when the user starts typing
func textViewDidBeginEditing(_ textView: UITextView) {

    // Story title text view
    if storyTitle.textColor == .gray {
        storyTitle.text = nil
        storyTitle.textColor = .black
    }

    // Story description text view
    if storyDescription.textColor == .gray {
        storyDescription.text = nil
        storyDescription.textColor = .black
    }
}
// To bring the placeholder back when the text views don't contain any text
func textViewDidEndEditing(_ textView: UITextView) {

    // Story title text view
    if storyTitle.text.isEmpty {
        storyTitle.text = "Add story title"
        storyTitle.textColor = .gray
    }

    // Story description text view
    if storyDescription.text.isEmpty {
        storyDescription.text = "Description of the story"
        storyDescription.textColor = .gray
    }
}

这里发生的是,当我点击一个文本视图时,两个文本视图中的占位符都消失了,这是因为同时执行了textViewDidBeginEditing下的两个if条件。如何防止这种情况发生?任何形式的帮助,我们感激不尽,谢谢:)

2 个答案:

答案 0 :(得分:1)

通过分配标签来查看对象(您的文本视图),您可以轻松地做到这一点。用 textview.tag = [some number]分配标签,然后您还可以在条件中添加以下内容:

storyDescription.tag = 1

func textViewDidBeginEditing(_ textView: UITextView) {
    if textView.tag == 1 && storyTitle.textColor == .gray {
        storyTitle.text = nil
        storyTitle.textColor = .black
    }
}

与其他textview类似

答案 1 :(得分:0)

// To remove placeholder text when the user starts typing
func textViewDidBeginEditing(_ textView: UITextView) {

    // Story title text view
    if storyTitle.textColor == .gray {
        storyTitle.text = nil
        storyTitle.textColor = .black
    }

    // Story description text view
    if storyDescription.textColor == .gray {
        storyDescription.text = nil
        storyDescription.textColor = .black
    }
}
// To bring the placeholder back when the text views don't contain any text
func textViewDidEndEditing(_ textView: UITextView) {

    // Story title text view
    if storyTitle.text.isEmpty {
        storyTitle.text = "Add story title"
        storyTitle.textColor = .gray
    }

    // Story description text view
    if storyDescription.text.isEmpty {
        storyDescription.text = "Description of the story"
        storyDescription.textColor = .gray
    }
}

这段代码将同时应用于您的两个TextViews。这是错误的,理想情况下,您应该使用委托方法中提供的textView,如下所示:

func textViewDidBeginEditing(_ textView: UITextView) {
    if textView.textColor == .gray {
        textView.text = nil
        textView.textColor = .black
    }
}
// To bring the placeholder back when the text views don't contain any text
func textViewDidEndEditing(_ textView: UITextView) {
    if textView.text.isEmpty {
        textView.textColor = .gray
    }
    // You can then restore the text depending on which TextView ended the edition, for example:
    if textView == self.storyTitle {
        self.storyTitle.text = "Add story title"
    }
}

顺便说一句,根据颜色确定TextView是否显示占位符可能不是最好的方法,特别是如果您打算最终支持装饰的字符串。完全是另一回事,但暂时仍会起作用