[英]Get UIWebView content's Height
我有一个使用loadHtmlString函数加载数据的uiwebview。事情是我从sqlite数据库加载数据,每次我加载不同长度的不同字符串,自然web视图获得不同的高度。
现在我需要知道每次uiwebview的确切高度,以便在webView下加载imageView。 我努力了
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;
NSLog(@"Size: %f, %f", fittingSize.width, fittingSize.height);
}
但我总是得到相同的结果。 我需要知道如何在每次加载时动态获取Web视图的高度。 谢谢
尝试这个
NSString *result = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"];
int height = [result integerValue];
在您的webViewDidFinishLoad方法中。
您可以通过访问Web视图的scrollView
属性来获取Web视图中内容的scrollView
:
NSLog(@"%f",myWebView.scrollView.contentSize.height);
这不是处理这种情况的正确方法; 而不是在图像视图中加载图像,并始终牢记高度和宽度以及所有这些东西......最简单,最可靠的解决方案就是将图像加载到html字符串中,就是这样! 只要确保baseURL如此,无论你采取什么方向,一切都会好的:
NSString* extension = @"gif";
NSString* strRR = [NSString stringWithFormat:@"%@.%@", authorNAme2,extension];
NSString *path = [[NSBundle mainBundle] bundlePath];
//NSLog(@"This is the path %@", path);
NSURL *baseURL = [NSURL fileURLWithPath:path];
// NSLog(@"this is the base URL %@",baseURL);
NSString *cachePath = [NSString stringWithFormat:@"%@/%@", path,strRR];
NSString * htmlString = [NSString stringWithFormat:@"\
<html>\
<table width='100%%'>\<tr>\<td>\
<body>\
<p style = 'font-size:30px;'> %@ <\p>\
</td></tr><tr><td><br></td></tr><tr><td align ='center'><br><img src='%@' width='60%%' \></td></tr></body>\
</table>\
</html>",authorNAme , cachePath];
// NSString *selection = [WebV2 stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"];
// NSLog(@"*/*/*/*/**/*/*/*/*/*/*//*/ This is your selection mate %@" , selection);
WebV.opaque = NO;
WebV.backgroundColor = [UIColor clearColor];
[self.WebV setScalesPageToFit:YES];
[self.WebV loadHTMLString:htmlString baseURL:baseURL];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.