按住退格键时的 UITextViewDelegate 行为

发布于 2024-11-07 11:51:01 字数 579 浏览 1 评论 0原文

我遇到了一个问题,当键盘上按住删除键时,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 技术交流群。

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

发布评论

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

评论(1

才能让你更想念 2024-11-14 11:51:01

雅各布提到我应该将此作为答案发布。所以就是这样。

我的解决方法是监视 shouldChangeTextInRange 中给出的文本长度和范围,然后将其与 textViewDidChange 中的文本长度进行比较。如果差异不同步,我会刷新支持文本缓冲区并从文本视图重建它。这不是最佳的。这是我的临时解决方法:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    //Push the proposed edit to the underlying buffer
    [self.editor.buffer changeTextInRange:range replacementText:text];

    //lastTextLength is an NSUInteger recording the length that
    //this proposed edit SHOULD make the text view have
    lastTextLength = [textView.text length] + ([text length] - range.length);

    return YES;
}

- (void)textViewDidChange:(UITextView *)textView
{
    //Check if the lastTextLength and actual text length went out of sync
    if( lastTextLength != [textView.text length] )
    {
        //Flush your internal buffer
        [self.editor.buffer loadText:textView.text];
    } 
}

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:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    //Push the proposed edit to the underlying buffer
    [self.editor.buffer changeTextInRange:range replacementText:text];

    //lastTextLength is an NSUInteger recording the length that
    //this proposed edit SHOULD make the text view have
    lastTextLength = [textView.text length] + ([text length] - range.length);

    return YES;
}

- (void)textViewDidChange:(UITextView *)textView
{
    //Check if the lastTextLength and actual text length went out of sync
    if( lastTextLength != [textView.text length] )
    {
        //Flush your internal buffer
        [self.editor.buffer loadText:textView.text];
    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文