iOS 具有内置功能,用户可以在其中设置文本替换
Settings > General > Keyboards > Text Replacement
在我的 Swift iOS 应用程序的 UITextField 中,我需要知道用户何时使用此文本替换,以便我可以采取适当的措施。我的直觉是使用
func textField(tf: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
但有趣的是,这种文本替换并没有调用这个函数,还有其他方法可以解决这个问题吗?
最佳答案
我在此设置中测试了 shouldChangeCharactersInRange
函数,它确实看到了文本扩展。我测试了:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
print(string.characters.count)
return true
}
您可以在文本扩展后立即看到字符数跳跃。
确保您的 textField 与 IBOutlet 连接,并且您已将该类设置为委托(delegate)(viewDidLoad
中的 textField.delegate = self
)。
一旦你看到这个工作,你当然可以识别文本扩展并以你希望的任何方式处理。
关于ios - Swift:检测 UITextField 中的 iOS 键盘文本替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36520158/