ios - Swift:检测 UITextField 中的 iOS 键盘文本替换

标签 ios swift uitextfield

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/

相关文章:

ios - 在 View Controller 转换中获取 subview 的最终帧

iphone - Spotify/iPod 喜欢现在播放 View

ios - 如何将带有表单数据的图像上传到api参数上的图像?

ios - 我可以让 UITextField 不可见吗?

ios - 如何禁用 UItextfield 上的替换、插入绘图、查找、学习、说话、说话..、暂停?

ios - 来自 NSString 的 NSUrl 返回 null

ios - 如何在基于导航的应用程序中返回 2 个 View

ios - 核心数据: How can I update a TableView field from an alert?

ios - 在 MapBox Android 和 iOS 上显示 GeoJSON

ios - 如何在 Objective C 中创建带有标签和文本字段的自定义单元格