退格键为HELD Down时的UITextViewDelegate行为 [英] UITextViewDelegate behaviour when backspace key is HELD Down

查看:156
本文介绍了退格键为HELD Down时的UITextViewDelegate行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,当按住键盘上的Delete键时,iOS给我的UITextViewDelegate不正确的信息.

I've run into a problem where iOS is giving my UITextViewDelegate incorrect information when the delete key is held on the keyboard.

当用户按住 iPad上的UITextView上的Delete键时,UITextView将开始按住的时间越长,则开始删除整个单词而不是单个字符(请注意:模拟器).

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).

发生这种情况时,UITextView委托方法:

When this happens, the UITextView delegate method:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

被调用时,其范围包括正确的光标位置,但长度为1.这是不正确的,因为UITextView现在正在删除整个单词,而不是单个字母.例如,以下代码将只打印一个空格.

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 " "

尽管UITextView删除了整个单词.替换文本正确地指定为空字符串.有人知道解决此问题的方法或解决方法吗?

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?

推荐答案

Jacob提到我应该将其发布为答案.所以就在这里.

Jacob mentioned I should post this as an answer. So here it is.

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

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];
    } 
}

这篇关于退格键为HELD Down时的UITextViewDelegate行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆