Rendering a UIWebView in drawRect with loadHTMLString

Posted by Nick Weaver on Stack Overflow See other posts from Stack Overflow or by Nick Weaver
Published on 2010-03-18T09:58:28Z Indexed on 2010/03/18 10:01 UTC
Read the original article Hit count: 1097

Filed under:
|
|
|

Hello there, I am having a problem with UIWebView. I'd like to render my own html code in it. When I add a webview as a subview and put in some html code it renders just fine. When it gets down to some optimized drawing of tableview cell with the drawRect method the problem pops up. Drawing UIView descendants works pretty well this way. It's even possible to load a URL with the loadRequest method, setting the delegate, conforming to the UIWebViewDelegate protocol and redrawing the table cell with setNeedsDisplay when webViewDidFinishLoad is called. It does show, but when it comes to loadHTMLString, nothing shows up, only a white rect. Due to performance reasons I have to do the drawing in the drawRect method.

Any ideas? Thanks in advance Nick

Example snippet code for the html code being loaded by a UIWebView:

NSString *html = @"<html><head><title>My fancy webview</title></head><body style='background-color:green;'><p>It somehow seems<h2 style='color:black;'>this does not show up in drawRect</h2>!</p></body></html>"; 
[webView loadHTMLString:html baseURL:nil];

Snippet for the drawRect method:

- (void)drawRect:(CGRect)aRect {
    CGContextRef context = UIGraphicsGetCurrentContext();

    [[webView layer] renderInContext:context];
}

© Stack Overflow or respective owner

Related posts about iphone

Related posts about uiwebview