i am trying to find out a solution when the return key of UIWebview is clicked ! i tried the following links but it's not working !
UIWebView, customize "return" key
How to detect keyboard enter key?
i want to do some offset work when user is typing on the UIWebview and return key is pressed !
Here is the code i am using !
- (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;
}
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.