1

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;
}
Community
  • 1
  • 1
iDeveloper
  • 498
  • 4
  • 9

2 Answers2

0

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];
Community
  • 1
  • 1
dcorbatta
  • 1,893
  • 19
  • 15
  • i am not sure about but may b because of ios 7. – iDeveloper Dec 02 '13 at 05:07
  • So check my another suggestion. In the meanwhile I will check in iOS 7. – dcorbatta Dec 02 '13 at 05:09
  • Well ! i don't want to add a new UITextview inside the webview as i am allowing images too in the webview it's like user can add text notes, and add images and bullets in the webview everything is working fine... the only thing i want is too check if user pressed return i have to offset the webview so as to avoid hidding text and better UI experience ! – iDeveloper Dec 02 '13 at 05:16
  • the above code is not working for me in ios 6 too !! :( :( @dcorbatta – iDeveloper Dec 02 '13 at 05:36
  • Maybe it's easy for front-end to do this than the native. When the cursor go into any input field, insert
    at the end of the webpage may let the webview to scroll.
    – Sabbath Dec 02 '13 at 06:23
  • what if the user is typing and press return key in order to go to new line ?? @Sabbath – iDeveloper Dec 02 '13 at 08:00
  • iDeveloper, sorry yesterday I lost conections. But the code of the first question works in iOS 6 and iOS 7, could you upload the HTML that you are using? Are you adding the script to detect the return key? – dcorbatta Dec 02 '13 at 09:04
  • Thank you so much !! it does work in my demo project.. i will implement in actual code. !! and let you know if i succeed !!:) :) – iDeveloper Dec 02 '13 at 12:26
  • You are a saviour to me.. i implemented in my original code and its working accordingly ! dcorbatta.. Thank you so much !!! – iDeveloper Dec 02 '13 at 12:49
  • i dnt have 15 reputation in stackoverflow so i can't up vote.. but i will surely up vote it... when i have the priviledge !:D dcorbatta – iDeveloper Dec 03 '13 at 04:21
-1

insert this code in textView: shouldChangeTextInRange:

if ([text isEqualToString:@"\n"]) {
    [textView resignFirstResponder];
    return NO;
}
Rugmangathan
  • 3,186
  • 6
  • 33
  • 44