UITextView 委托方法 textView:shouldChangeTextInRange:replacementText: 在快速删除时给出错误的 replacementText

发布于 2025-01-06 00:39:20 字数 229 浏览 1 评论 0原文

我有一个 UITextView,并将所有文本视图更改事件记录到一个数组中。

当用户快速删除时(按住删除键一次删除一个单词而不是一个字符),当调用textView:shouldChangeTextInRange:replacementText方法时,范围变量的长度仅为1,而实际上范围应该是单词中删除的字母数。有谁知道这个 Apple Bug 的任何解决方法,以便我可以正确识别 UITextView 中更改的位置和范围以及文本?

I have a UITextView and I am recording all the text view change events into an array.

When the user is fast deleting (holding down the delete key so words at deleted at a time and not just one character), when the textView:shouldChangeTextInRange:replacementText method is called, the range variable's length is only 1, when in reality the range should be the number of letters deleted in the word. Does anyone know of any work-arounds to this Apple Bug so I can correctly identify the location and range and the text changed in the UITextView?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫弦 2025-01-13 00:39:20

不,这不是一个错误。每次文本更改时都会调用此函数。可能您正在使用调试模式并使用键盘给予退格键。它会在命中断点之前生成多个事件。因此,该函数将被多次调用。

No, its not a bug. This function will get call for each time your text is changing. Probably you are working with debug mode and giving backspace with keyboard. It will generate multiple event before hitting the breakpoint. So, the function will get call multiple times.

半衾梦 2025-01-13 00:39:20

正如您在问题中提到的,即使删除整个单词,“范围”也始终为 1。
您必须检查范围“range.length”的长度,这将为您提供从 UITextView 中删除的字符的准确计数。
我有同样的场景,并基于它做了一些工作,这将只允许一次删除单个字符而不是整个单词:

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if([text isEqualToString:@""]) {
        // Check if length of range being deleted is less than 1 then only delete it
        if (range.length > 1) {
            return NO;
        } else
            return YES;
    } else {
        return YES;
    }
}

As you have mentioned in your question the 'range' is always 1 even if a whole word is deleted.
You have to check the length of range 'range.length' which will provide you exact count of characters being deleted from your UITextView.
I had same scenario and did some work around based on it which will only allow to delete a single character at once not the whole word:

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if([text isEqualToString:@""]) {
        // Check if length of range being deleted is less than 1 then only delete it
        if (range.length > 1) {
            return NO;
        } else
            return YES;
    } else {
        return YES;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文