Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
871 views
in Technique[技术] by (71.8m points)

ios - UIWebView, customize "return" key

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.

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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

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

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

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

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

to allow the return key to function normally in textareas.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...