2

I am using a UIWebView for creating a rich text editor. This web view is placed inside a custom cell of a UITableView

I need to change cell size when typing inside the webview. I want to get webview height when I am typing. There is no scrolling inside the webview.

This is the webview initial content,

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

How do I change the UIWebView height when typing?

Amar
  • 13,202
  • 7
  • 53
  • 71
Suravi
  • 301
  • 1
  • 7
  • 21

2 Answers2

0

because of UIWebView is a subview of UITableViewCell if you want to increase the size of webview you have to increase the size of UITableView ,please see this thread this is nearer to what you want.

Can the height of a UITableViewCell be changed without reloading the table view?

increase uitableviewcell height simultaneously increasing the inner UITextView

Thanks.

Community
  • 1
  • 1
Banker Mittal
  • 1,918
  • 14
  • 26
  • I want to get UIwebview Height according to typing.I need to know how to get uiwebview when typing. after that i can change table view height. – Suravi Oct 24 '13 at 11:37
  • you can get it using webView.scrollView.contentSize.height. – Banker Mittal Oct 24 '13 at 11:41
  • Yes thank you it work.when I typing it increase height.but when I deleting text, it not reduce height.why? – Suravi Oct 25 '13 at 04:13
0

I know this was asked a while ago but I was working on this and I discovered the way to handle this. In short, the best way to handle this is by using JavaScriptCore.framework in the following way

    let context = self.webView.valueForKeyPath("documentView.webView.mainFrame.javaScriptContext") as JSContext
    var callbackHandler : @objc_block (JSValue) -> Void = { (val : JSValue) -> Void in
        NSLog("Got callback")
    }
    context.setObject(unsafeBitCast(callbackHandler, AnyObject.self), forKeyedSubscript: "callApp")
    context.evaluateScript("document.getElementById('content').addEventListener('input', callApp, false);")

What this code does is the following:

  • Create a connection between the UIWebView and Swift
  • Add a Swift handler to a JavaScript function callApp
  • Update your editable div to call the JavaScript function callApp when the input changes

When this is hooked up, every key press you enter in the UIWebView will fire the callbackHandler in Swift. You can then use this to resize the UIWebView as appropriate.

tng
  • 4,286
  • 5
  • 21
  • 30