views:

309

answers:

1

Hey.

Oh, and I should probably mention that I am not loading the UIWebView with a website, but with an HTML string.

I'm trying to use a UIWebView for displaying content higher than the screen of the iPhone, without needing to scroll in the webView itself.

For that, I need a way to get the total document size, including the scrollable area. I have tried a number of different Javascript solutions:

(document.height !== undefined) ? document.height : document.body.offsetHeight // Returns height of UIWebView
document.body.offsetHeight // Returns zero
document.body.clientHeight // Returns zero
document.documentElement.clientHeight // Returns height of UIWebView
window.innerHeight // Returns height of UIWebView -2
document.body.scrollHeight // Returns zero

Can you think of any other way to do it? Thanks.

EDIT: Can you use sizeThatFits: here?

Current (nonworking) code:

[[[self.singlePost.contentText subviews] lastObject] setScrollEnabled:NO];
int content_height = [[self.singlePost.contentText stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"] intValue];
NSLog(@"Content_height: %d", content_height);
CGRect rect = self.singlePost.contentText.frame;
rect.size.height = content_height;
self.singlePost.contentText.frame = rect;
+1  A: 

Where do you call your code? For me it returns 0 if it is called right after the loadHTMLString Method. If I call it in the (void)webViewDidFinishLoad:(UIWebView *)theWebView delegate, I get a valid value.

- (void)loadHTML: (NSString *)html {
    webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    webView.delegate = self;

    NSURL *resourceURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    [webView loadHTMLString:html baseURL: resourceURL];
    NSLog(@"height: %d", [[webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"] intValue]); // returns 0
  }

- (void)webViewDidFinishLoad:(UIWebView *)theWebView {
    NSLog(@"height: %@", [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"]); //return 2166
}
robert
Hah, as simple as that, huh? Well, thanks :)
Emil