按住退格键时的 UITextViewDelegate 行为
我遇到了一个问题,当键盘上按住删除键时,iOS 给我的 UITextViewDelegate 提供了不正确的信息。
当用户按住 iPad 上 UITextView 上的删除键时,按住的时间越长,UITextView 将开始删除整个单词而不是单个字符(注意:这不会在模拟器中发生)。
发生这种情况时,UITextView 委托方法:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
使用由正确的光标位置组成的范围(但长度为 1)调用。这是不正确的,因为 UITextView 现在删除整个单词,而不是单个字母。例如,以下代码将仅打印一个空格。
[textView substringWithRange:range]
string contains " "
尽管 UITextView 删除了整个单词。替换文本正确地给出为空字符串。有谁知道这个问题的解决方案或解决方法?
I've run into a problem where iOS is giving my UITextViewDelegate incorrect information when the delete key is held on the keyboard.
When the user HOLDS the delete key on a UITextView on an iPad the UITextView will begin to delete entire words instead of individual characters the longer it is held down (note: this does not occur in the simulator).
When this happens, the UITextView delegate method:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
Gets called with a range consisting of the correct cursor location, but a length of 1. This is incorrect as the UITextView is now deleting entire words, not single letters. The following code, for example, will print only a single space.
[textView substringWithRange:range]
string contains " "
Despite the UITextView removing a whole word. The replacement text is correctly given as the empty string. Does any one know of a solution or workaround to this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
雅各布提到我应该将此作为答案发布。所以就是这样。
我的解决方法是监视 shouldChangeTextInRange 中给出的文本长度和范围,然后将其与 textViewDidChange 中的文本长度进行比较。如果差异不同步,我会刷新支持文本缓冲区并从文本视图重建它。这不是最佳的。这是我的临时解决方法:
Jacob mentioned I should post this as an answer. So here it is.
My hackish workaround to this is to monitor the text length and range given in shouldChangeTextInRange, and then compare it to the length of the text in textViewDidChange. If the differences are out of sync I flush my backing text buffer and rebuild it from the text view. This is not optimal. Here is my temporary workaround: