Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confirm UITextview auto-complete

This seems impossible, but maybe someone else has had the same problem.

Is it possible for me to accept an autocomplete programmatically, or in some way get the suggested word that pops up? My problem is that I'm capturing the return/backspace keystroke and then move focus to another textview. When enter/backspace is hit, the textview will ignore the auto-suggested word. It seems that it is only possible to accept an autocompletion by hit space/dot (and return for new row). With this code:

 - (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
                                replacementText:(NSString *)text {
    NSRange textViewRange = [textView selectedRange];

    // Handle newrow and backspace.  
    if(([text length] == 0) && (textViewRange.location== 0) && textViewRange.length==0){
        // BACKSPACE KEYSTROKE
        [delegate doSomethingWhenBackspace];
        return NO;      
    }else if ([text isEqualToString:@"\n"]){    
        // RETURN KEYSTROKE
        [delegate doSomethingWhenReturn];       
        return NO;      
    }

    return YES;
}

I tried to programmatically add "space" when the return key is hit but that also ignores the auto-completed word.

else if ([text isEqualToString:@"\n"]){ 
                // Tryin to accept autocomplete with no result. 
                textview.text = [textview.text stringByAppendingString:@" "];
            // RETURN KEYSTROKE
            [delegate doSomethingWhenReturn];       
            return NO;      
        }

Any suggestions?

like image 793
f0rz Avatar asked Dec 14 '11 09:12

f0rz


1 Answers

Call -resignFirstResponder (i.e. [textView resignFirstResponder]) on the text view or text field which needs to accept autocomplete results: UIKit will change the .text property to include the autocorrected text.

If you want to keep the keyboard up after your first view resigns first responder, pass the firstResponder responsibility onto your next text input view with [anotherTextView becomeFirstResponder].

like image 131
cbowns Avatar answered Nov 13 '22 02:11

cbowns