views:

197

answers:

1

I'm making a custom input method for the iPad, I want to be able to replace the system keyboard with my input method and enter text via that input method.

According to the documentation all I need to do is to set the inputView property with my view and it will be used instead of the system keyboard. I did that and it works, as far as showing the keyboard but how do I actually enter text into the text view?

Supposedly the text view needs to adopt the UIKeyInput and I can use the protocol's methods to enter the text but in reality UITextView doesn't adopt this protocol. conformsToProtocol:@protocol(UIKeyInput) returns NO and "deleteBackwards" is not implemented (insertText and hasText are implemented. In addition to that, "insertText" doesn't cause the textViewDidChange: delegate method to be invoked. Obviously I need to send the UIKeyInput method to some other object (a field editor?) but how do I get it?

Any ideas?

+1  A: 

Assuming your keyboard has some buttons, why cant you just set a selector for your keys, and append to the textViews text when each button is clicked, I have done this an it works fine...Here is the method that actually does the "writing" to the UITextView, this method is part of a custom protocol defined by the inputView and is called on the delegate whenever a button is pressed, hope it helps, note: i submit ret when the return key is pushed and <- when backspace is pushed.

-(void)userDidInputString:(NSString*)s
{
    NSRange r=padView.textView.selectedRange; 
    if([s isEqualToString:@"ret"])
        s=@"\n";
    if([s isEqualToString:@"<-"])
    {
        NSString *text=padView.textView.text;   
        if(r.location>0)
        {
            r.location=r.location-1;
            r.length+=1;
        }
                [padView.textView setScrollEnabled:YES];
        padView.textView.text=[text stringByReplacingCharactersInRange:r   withString:@""];
        [padView.textView setScrollEnabled:NO];
        r.length=0;
        [padView.textView setSelectedRange:r];
        [note setNoteText:padView.textView.text];

    }
    else {
        NSString *text=padView.textView.text;   

        [padView.textView setScrollEnabled:YES];
        padView.textView.text=[text stringByReplacingCharactersInRange:r withString:s];
                [padView.textView setScrollEnabled:NO];
        r.location=r.location+[s length];
        //whenever you modify the text by setting the UITextViews text property it resets the cursor to the end of the text view, we have this line below to go back to where the user left off
        [padView.textView setSelectedRange:r];


            }


}
Daniel
Thanks. This solutions is nice but I'd like the text view to actually do the editing (and behave normally, complete with autocorrection and so forth) and not edit the text myself.
Eyal Redler
I called DTS about this and it seems that your solution is indeed the only option. Their answer was that UIKeyInput (and UITextInput) is not for client (that is, you're not supposed to ever invoke these methods yourself) but only for views that wish to receive keyboard input.
Eyal Redler