简体   繁体   中英

stringByEvaluatingJavaScriptFromString recognize webview content height

i placed a Uiwebview in a UIscrollview because after the uiwebview i want to display some images with the PageController.

i'm using this line of code.

   -(void)webViewDidFinishLoad:(UIWebView *)webView{
    if (webView!=WebView)return;
    float h;

    NSLog(@"web view is %f high", WebView.frame.size.height);
    NSString *heightString = [WebView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"body\").offsetHeight;"];
    NSLog(@"web content is %@ high",heightString);

    h = [heightString floatValue] +12.0f;   
    WebView.frame = CGRectMake(WebView.frame.origin.x, WebView.frame.origin.y, WebView.frame.size.width, h);

    h = WebView.frame.origin.y + h + 20; 
    [ScrollView setContentSize:CGSizeMake(320, h)];


}

But Web view is %f high doesn't work, it does not reconize the height of the WebView does anyone know how i can fix this? Is this because i'm using [WebView loadHTMLString:html baseURL:nil];

And: NSString *heightString = [WebView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\\"body\\").offsetHeight;"];

 NSString *html = [NSString stringWithFormat:@"<html><head><!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\"><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /><style>body {  font: normal 13px helvetica; color: #565656;   } </style></head><body><p>%@</p></body></html> ",text];  


[WebView loadHTMLString:html baseURL:nil];

Try

NSString *heightString = [Bericht stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"];

Good luck, Nathan

UIWebView already contains a scrollView, why are you adding it as a subview again. Check Apple's documentation here

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM