UITextViewDelegate behaviour when backspace key is HELD Down

我的未来我决定 提交于 2019-12-05 00:59:08

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/6007997/uitextviewdelegate-behaviour-when-backspace-key-is-held-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!