UITextView inputView [英] UITextView inputView

查看:149
本文介绍了UITextView inputView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为iPad制作自定义输入法,我希望能够用我的输入法替换系统键盘,并通过该输入法输入文字。

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.

根据文档,我需要做的就是用我的视图设置inputView属性,它将被用来代替系统键盘。我这样做了,它可以工作,就显示键盘但我如何在文本视图中实际输入文本?

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?

据说文本视图需要采用UIKeyInput和我可以使用协议的方法输入文本,但实际上UITextView不采用这个协议。 conformsToProtocol:@protocol(UIKeyInput)返回NO并且未实现deleteBackwards(insertText和hasText已实现。除此之外,insertText不会导致 textViewDidChange:要调用的委托方法。显然我需要将 UIKeyInput 方法发送给其他对象(字段编辑器? )但我怎么得到它?

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?

任何想法?

推荐答案

假设你的键盘有一些按钮,为什么你不能只为你的键设置一个选择器,并在单击每个按钮时附加到textViews文本,我已经做到这一点它工作正常...这是实际做的方法写入到UITextView,这个方法是inputView定义的自定义协议的一部分,每当按下一个按钮时在委托上调用,希望它有所帮助,注意:我在按下返回键时提交ret并且< - 当按下退格键时。

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


            }


}

这篇关于UITextView inputView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆