UIWebView,自定义“返回”键 [英] UIWebView, customize "return" key

查看:127
本文介绍了UIWebView,自定义“返回”键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在UIWebView的键盘中自定义return键。通常你可以通过设置UITextField的returnKey属性来做到这一点,但因为应用程序是HTML格式,所以没有UITextFields,只有textareas。

I'd like to customize the "return" key in the UIWebView's keyboard. Normally you can do this by setting the returnKey property of a UITextField, but because the application is in HTML, there are no UITextFields, just textareas.

目前,我的工作是隐藏右下角的替换按钮,并用键盘向上动画,使其位于返回键的顶部。实际上它看起来非常好,但是我担心我的代码可能无法在iOS的未来版本中运行

Currently, my work around is to hide my replacement button in the bottom right hand corner and animate it upwards with the keyboard so that it sits ontop of the return key. It looks very nice, actually, but I'm concerned that my code might fail to work in future versions of iOS

有没有更好的方法来做到这一点?我知道在iOS4中有一种方法可以访问UIKeyBoard,但是iOS5删除了它。

Is there a better way to be doing this? I know in iOS4 there was a way to acces the UIKeyBoard, but iOS5 removed this.

推荐答案

要查看如果触摸了返回键我将onKeyPress添加到HTML body标签中,如下所示

To see if the Return key was touched I add onKeyPress to the HTML body tag like this

<body onKeyPress="return returnKeyPressed(event)">

页面上有一个javascript函数,看起来像这样

There is a javascript function on the page that looks like this

function returnKeyPressed(event){
    if(window.event.keyCode == 13) document.location = "returnkeypressed:";
    return true;
}

我在班级中有这个是webView委托

And I have this in the class that is the webView delegate

- (BOOL)webView:(UIWebView*)aWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{
    NSString *requestString = [[request URL] absoluteString];
    NSArray *components = [requestString componentsSeparatedByString:@":"];
    NSString *theTask = (NSString *)[components objectAtIndex:0];

    if([theTask isEqualToString:@"returnkeypressed"]) [aWebView endEditing:YES];
}

这只会结束webView中的所有编辑,解除键盘并移除焦点来自任何特定的textarea或输入。

This just ends all editing in the webView, dismisses the keyboard, and removes focus from any specific textarea or input.

如何在webView情境中更改Return键的标签(类似于完成)仍然是个谜。我。想法?

How to change the "label" of the Return key in a webView situation (to something like 'Done') is still a mystery to me. Ideas?

更新:
我将javascript函数更改为此

UPDATE: I changed the javascript function to this

function returnKeyPressed(event){
    if(event.srcElement.nodeName == 'INPUT' && window.event.keyCode == 13) document.location = "returnkeypressed:";
    return true;
}

允许返回键在textareas中正常运行。

to allow the return key to function normally in textareas.

这篇关于UIWebView,自定义“返回”键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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