UIWebview keyboard return key pressed ios 7

浪尽此生 提交于 2019-12-03 21:26:39
dcorbatta

Why the answer of UIWebView, customize "return" key didn't work?

I try that and work for me, I only change the method shouldStartLoadWithRequest: to:

- (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];
        return NO;
    }
    return YES;
}

Another way is using a buffer text view, like my answer for change the keyboard.

Edit: you need add the script to detect the return key:

NSString* plainContent = @"Edit here.....";

NSString* htmlContentString = [NSString stringWithFormat:
                               @"<html>"
                                "<head>"
                                    "<script>"
                                        "function returnKeyPressed(event){"
                                            "if(window.event.keyCode == 13)"
                                                "document.location = \"returnkeypressed:\";"
                                            "return true;"
                                        "}"
                                    "</script>"
                                "</head>"
                                "<body onKeyPress=\"return returnKeyPressed(event)\">"
                                    "<div id=\"content\" contenteditable=\"true\" style=\"font-family: Arial\">%@"
                                    "</div>"
                                "</body>"
                               "</html>",
                               plainContent];

insert this code in textView: shouldChangeTextInRange:

if ([text isEqualToString:@"\n"]) {
    [textView resignFirstResponder];
    return NO;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!