我想在点击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;
}
答案 0 :(得分:0)
为什么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];
答案 1 :(得分:-1)
在textView: shouldChangeTextInRange:
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}