开发者

UITextView inputView

开发者 https://www.devze.com 2023-01-12 19:10 出处:网络
I\'m making a custom in开发者_运维百科put 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.

I'm making a custom in开发者_运维百科put 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?


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


            }


}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号