在 UITextField 中粘贴文本时的奇怪行为

Strange Behavior When Pasting Text in UITextField

这是我所做的:

  1. 打开 App Store 应用程序
  2. 在搜索框中输入“123”
  3. 从搜索框中删除文本“123”
  4. 在搜索框中输入 "xx"
  5. 将搜索框光标移动到两者之间"x"
  6. 将“123”粘贴到搜索框

结果应该是"x123x",但是我得到的是"x 123 x"。这发生在所有应用程序的所有 UITextField 上。

请参考这张动图: demo

这是 iOS 错误吗?或者我在这里遗漏了什么?

这是有意为之的行为。有一个 属性 可以在 iOS 11:

上禁用它
if #available(iOS 11.0, *) {
    textField.smartInsertDeleteType = .no
}

另请参阅:https://developer.apple.com/documentation/uikit/uitextinputtraits/2865828-smartinsertdeletetype?changes=_6