UITextView 的占位符文本与 First Responder 混合

Placeholder Text for UITextView mixed w/ First Responder

我目前有一个带有占位符文本的文本视图,每当用户点击文本视图时,该文本视图就会消失,并且如果文本视图为空,则每当第一响应者辞职时,文本视图中的文本就会重新出现。 (这是我使用的代码,以防有人想使用它)

*注意,首先将textview的文字颜色设置为浅灰色,并设置占位符文字。然后使用这些方法:

func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
    //If it begins editing, then set the color to black
    if (textView.tag == 0){
        textView.text = ""
        textView.textColor = .black
        textView.tag = 1
    }
    return true
}


func textViewDidEndEditing(_ textView: UITextView) {
    if textView.text.isEmpty {
        textView.text = "Example: I started my career as a street wear model based in Maryland. After 3 years of working with some of the top companies there, I moved to LA, where I currently reside. I’ve been featured in shows, 12 magazines, commercials, and a number of music videos. Now, Im currently looking to continue working with clothing companies and campaigns."
        textView.textColor = .lightGray
        textView.tag = 0
    }
}

我想更上一层楼。现在,只要文本视图成为第一响应者,文本就会消失。 我希望文本在用户真正开始输入时消失,而不仅仅是在选择文本视图时消失。当屏幕出现时,我自动将第一响应者设置为文本视图并且我计划保持这种状态。但是因为它是自动设置的,所以您看不到占位符文本。 我只希望用户按下某个键时文本视图消失,而不是因为它被选中。

假设您有这样的占位符文本,

let placeholderText = "Example: I started my career as a street wear model based in Maryland. After 3 years of working with some of the top companies there, I moved to LA, where I currently reside. I’ve been featured in shows, 12 magazines, commercials, and a number of music videos. Now, Im currently looking to continue working with clothing companies and campaigns."

并且在调用 textViewbecomeFirstResponder 之前,您在 storyboardviewDidLoad 中将此文本设置为 textView

然后在这两个委托方法中,您可以实现如下行为,

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if textView.text == placeholderText {
        textView.text = ""
    }
    return true
}

func textViewDidEndEditing(_ textView: UITextView) {
    if textView.text.isEmpty {
        textView.text = placeholderText
    }
}

目前您正在清除 textViewShouldBeginEditing 中的文本,这是您看不到该文本的主要原因。您应该删除那里的清除文本,但您可以按原样继续更改颜色等。

这是代码

class ViewController: UIViewController {

    @IBOutlet weak var textView: UITextView!
    let placeholder = "Example: I started my career as a street wear model based in Maryland. After 3 years of working with some of the top companies there, I moved to LA, where I currently reside. I’ve been featured in shows, 12 magazines, commercials, and a number of music videos. Now, Im currently looking to continue working with clothing companies and campaigns."
    let start = NSRange(location: 0, length: 0)
    override func viewDidLoad() {
        super.viewDidLoad()
        textView.delegate = self
        textView.text = placeholder
    }
}

extension ViewController: UITextViewDelegate {
    func textViewDidChangeSelection(_ textView: UITextView) {
        // Moves cursor to start when tapped on textView with placeholder
        if textView.text == placeholder {
            textView.selectedRange = start
        }
    }
    func textViewDidChange(_ textView: UITextView) {
        // Manages state of text when changed
        if textView.text.isEmpty {
            textView.text = placeholder
            textView.textColor = .lightGray
        } else if textView.text != placeholder {
            textView.textColor = .black
        }
    }
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        // Called when you're trying to enter a character (to replace the placeholder)
        if textView.text == placeholder {
            textView.text = ""
        }
        return true
    }
}

更新 #1:最大长度

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    // Called when you're trying to enter a character (to replace the placeholder)
    if textView.text == placeholder {
        textView.text = ""
    } else if textView.text.count >= 175 && text.count > 0 {
        // Now it can delete symbols but can't enter new
        return false
    }
    return true
}

尝试通过按住擦除按钮清除 textView 时出现小错误。占位符未显示。方法 textViewDidChange 以某种方式未被调用(与 shouldChangeTextIn 不同步)。可能的解决方法