I have a uiwebview that loads data using the loadHtmlString function.The thing is i am loading the data from an sqlite database, each time i load a different string with different length and naturally the web view obtains different height.
Now i need to know each time the exact height of the uiwebview in order to load an imageView right under the webView.
I have tried
- (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);
}
but i always get the same result. I need to know how can i get dynamically the height of web view each time it is loaded. Thx
Try this
NSString *result = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"];
int height = [result integerValue];
in your webViewDidFinishLoad method.
You can get the height of the content in a web view by accessing the web view's scrollView
property:
NSLog(@"%f",myWebView.scrollView.contentSize.height);
This wasn't the right approach to handle this situation; instead of loading the image in an image view and having to keep always in mind the height and width and all that stuff...The easiest and most reliable solution was just to load the image inside the html string and that's it ! Just be sure of the baseURL as so and everything will be ok no matter what orientation you take :
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];