UITextView委托多次调用

Sur*_*esh 5 uitextview uitextviewdelegate ios swift

我正在使用 UITextView 并实现委托功能

var count = 0
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    print(text)
    count += 1
    print(count)
    return true
}
Run Code Online (Sandbox Code Playgroud)

样本

在此输入图像描述

当我从键盘选择预测文本时,shouldChangeTextInRange 委托调用两次。

  1. 为什么这位代表打电话两次?
  2. 为什么仅使用预测文本就会发生这种情况

Ale*_*son 0

每当用户键入新字符或删除现有字符时,文本视图都会调用此方法。该方法的实现是可选的。您可以使用此方法在将文本提交到文本视图存储之前替换文本。例如,拼写检查器可能会使用此方法将拼写错误的单词替换为正确的拼写。

来自苹果文档https://developer.apple.com/reference/uikit/uitextviewdelegate/1618630-textview