【问题标题】:UIWebview keyboard return key pressed ios 7UIWebview键盘返回键按下ios 7
【发布时间】:2013-12-02 04:53:09
【问题描述】:

我正在尝试在单击 UIWebview 的返回键时找出解决方案! 我尝试了以下链接,但它不起作用!

UIWebView, customize "return" key

How to detect keyboard enter key?

当用户在 UIWebview 上键入并按下返回键时,我想做一些偏移工作!

这是我正在使用的代码!

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString* plainContent = @"Edit here.....";
    NSString* htmlContentString = [NSString stringWithFormat:
                                   @"<html>"
                                   "<body>"

                                   "<div id=\"content\" contenteditable=\"true\" style=\"font-family: Arial\">"
                                   "%@"
                                   "</div>"

                                   "</body></html>", plainContent];

    [_webview loadHTMLString:htmlContentString baseURL:nil];
}
- (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([[[request URL] absoluteString] rangeOfString:@"enterClicked"].location!=NSNotFound)    // When "enterClicked" found
    {
        NSLog(@"enterClicked !");
        //Do your desired work
        return NO;
    }
    if([theTask isEqualToString:@"returnkeypressed"]){
        NSLog(@"theTask");
        [aWebView endEditing:YES];
        return NO;
    }
    return YES;
}

【问题讨论】:

    标签: ios iphone objective-c uiwebview ios7


    【解决方案1】:

    为什么UIWebView, customize "return" key的回答没有用?

    我尝试并为我工作,我只将方法 shouldStartLoadWithRequest: 更改为:

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

    另一种方法是使用缓冲区文本视图,例如我的answer 来更改键盘。

    编辑:您需要添加脚本来检测返回键:

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

    【讨论】:

    • 我不确定,但可能是因为 ios 7。
    • 所以请检查我的另一个建议。与此同时,我将检查 iOS 7。
    • 好吧!我不想在 webview 中添加一个新的 UITextview,因为我也允许在 webview 中使用图像,就像用户可以添加文本注释,并在 webview 中添加图像和项目符号一样一切正常......我唯一想要的太检查用户是否按下返回我必须偏移 webview 以避免隐藏文本和更好的 UI 体验!
    • 以上代码在 ios 6 中也不适用于我!! :( :( @dcorbatta
    • 也许前端比原生更容易做到这一点。当光标进入任意输入框时,在网页末尾插入
      可以让webview滚动。
    【解决方案2】:

    将此代码插入textView: shouldChangeTextInRange:

    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    

    【讨论】:

    • 我已经尝试过了,发现它无法正常工作,因为 UIWebview 只能识别 DOM。
    猜你喜欢
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多