因此,我在UITextView中添加了占位符功能,现在它将输入文本的前两个字符大写,而不是原来的大写。

如果您能以任何方式提供帮助,我将感谢您在解决此问题方面的帮助。我一直在尝试,但是似乎一无所获。

https://github.com/sidyakinian/DoubleCapitalizationBug –这是一个展示错误的项目。或者,如果您喜欢这里的代码:

class ViewController: UIViewController {

    @IBOutlet weak var textView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        textView.delegate = self
        textView.text = "Type something"
        textView.textColor = UIColor.rgb(197, 197, 199)
    }

}

extension ViewController: UITextViewDelegate {

    // Put cursor at the beginning of TextView when editing begins
    func textViewDidBeginEditing(_ textView: UITextView) {
        textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument)
    }

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        // Sets placeholder for UITextView
        let updatedText = (textView.text as NSString).replacingCharacters(in: range, with: text)
        if updatedText.isEmpty {
            textView.text = "Type something"
            textView.textColor = UIColor.rgb(197, 197, 199)
            textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument)
        } else if textView.textColor == UIColor.rgb(197, 197, 199) && !text.isEmpty {
            textView.textColor = UIColor.black
            textView.text = text
        } else {
            return true
        }

        return false
    }

    // Prevents selecting the placeholder
    func textViewDidChangeSelection(_ textView: UITextView) {
        if self.view.window != nil {
            if textView.textColor == UIColor.rgb(197, 197, 199) {
                textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument)
            }
        }
    }

}


谢谢!

最佳答案

下面的代码解决了文本视图中前2个字母的大写问题。

func textViewDidChange(_ textView: UITextView) {
    if textView.text.isEmpty {
        textView.text = "Type something"
        textView.textColor = self.placeholderTextColor
    } else {
        textView.textColor = .black
    }
}

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if text.isEmpty {
        let updatedText = (textView.text as NSString).replacingCharacters(in: range, with: text)
        if updatedText.isEmpty {
            textView.text = "Type something"
            textView.textColor = self.placeholderTextColor
            textView.selectedRange = NSRange(location: 0, length: 0)
        }
    } else {
        if textView.text == "Type something" {
            textView.text = ""
        }
        textView.textColor = .black
    }
    return true
}


文本视图在文本视图中大写前2个字符的原因是,您在下面的委托方法中返回了false


  func textView(_ textView:UITextView,shouldChangeText范围:NSRange,replaceText文本:String)-> Bool

关于swift - TextView用大写的前两个字符代替一个,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58583806/

10-16 07:41