Confirm UITextview auto-complete
Asked Answered
J

3

8

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?

Jorgenson answered 14/12, 2011 at 9:42 Comment(0)
C
4

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

Cammiecammy answered 1/2, 2012 at 23:2 Comment(0)
H
0

For backspace and space u can use this condition

if ([[text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0)
{    
 [delegate doSomethingWhenBackspace];    
 return NO;    
}
Highup answered 27/12, 2011 at 12:15 Comment(0)
H
-4

I've had a very similar problem, I was making an app that had to read every letter in a text view and I has issues when Autocomplete inserted words because it was saving it as if it was one letter. you could add each character to an array and then check to see if any are over 1 string in length. Or you could add each character that is put in into an array and then run something like

NSString *string = text;
NSMutableArray *array = [NSMutableArray new];
for (int i=0; i<string.length; i++) {
 [array addObject:[string substringWithRange:NSMakeRange(i, 1)]];
}

to add each character individually, by comparing the two arrays you could determine if autocorrect has been used and with what word/s. Hope this will help.

Hyland answered 22/12, 2011 at 10:37 Comment(1)
This is a truly terrible way to accomplish this. Change UITextView/UITextField's firstResponder properties instead to have UIKit do autocomplete work for you.Cammiecammy

© 2022 - 2024 — McMap. All rights reserved.