How to calculate web view content height in iOS? - ios

I need to resize the web view frame according to content height. So I need to calculate the height of content.

This is the solution that i use
NSUInteger contentHeight = [[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"document.body.scrollHeight;"]] intValue];
Your webview should have a frame smaller than all possible content size

Try This,
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGSize contentSize = webView.scrollView.contentSize;
NSLog(#"Content Size %#", NSStringFromCGSize(contentSize));
}

Related

Hot to increase height of contentSize for wkWebView?

Hot to increase height of contentSize for wkWebView?
Tried this but doesn't work:
[[self.webView scrollView]setContentSize:CGSizeMake([[[self webView]scrollView] contentSize].width, [[[self webView]scrollView] contentSize].height+300)];
Technically it increased but practically still can't scroll more.
It works when you try to increase content size after webView finish load try this:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[[self.webView scrollView]setContentSize:CGSizeMake([[[self webView]scrollView] contentSize].width, [[[self webView]scrollView] contentSize].height+100)];
}

Unable to fit webpage in UIWebView in ios?

I am displaying a webpage in UIWebview. My webpage looks good on the browser, but on iPhone 5s the view does not fit. It is much too large, so I want to know how I can fit the content.
I have used ScalePageToFit & sizeTofit but nothing is working.
set your web view delegate and frame.
yourwebview.frame=[[UIScreen mainScreen] bounds];
yourwebview.delegate = self;
end then use this delegate method to set height.
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGSize contentSize = aWebView.scrollView.contentSize;
NSLog(#"webView contentSize: %#", NSStringFromCGSize(contentSize));
yourwebview.contentsize = contentSize;
}
OR
also See my answer at How to make iOS UIWebView display a mobile website correctly?
lets try this, some web pages doesn't responds to scalesPageToFit try this not sure , just give it a try
//those web pages can handle only once the web page is loaded completely
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if ([_webView respondsToSelector:#selector(scrollView)])
{
UIScrollView *scroll = [_webView scrollView];
float zoom = ((_webView.bounds.size.width + 20 )/scroll.contentSize.width) - 0.30;
[scroll setZoomScale:zoom animated:YES];
}
}

Screenshot UIWebView’s full content in IBAction

Hello currently I have the code in an IBAction which saves a screenshot of the UIWebView, what I want it for it to take a screenshot of the full content of the webpage (both what is visible and what is not)
So far I have managed to get it to take a screenshot of the size of the full content with the visible content showing, however the rest of the screenshot where the non-visible content is is white.
CGSize layerSize = [_myWebView sizeThatFits:myWebView.scrollView.contentSize];
if ([UIScreen instancesRespondToSelector:#selector(scale)] && [[UIScreen mainScreen] scale] == 2.0f) {
UIGraphicsBeginImageContextWithOptions(layerSize, NO, 2.0f);
UIGraphicsBeginImageContext(layerSize);
} else {
UIGraphicsBeginImageContext(layerSize);
}
[_myWebView.scrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, self, nil, nil);
What can I do to make all the content visible in the screenshot?
This is a guess:
Try adjusting the frame.size of the webView to be equal to the scrollView contentSize. Then, reload the webview and call renderInContext: after the reload is complete. See what that gives you.
If the frame of the webview is equal to the superview(screen) then the webview wont render the content which is out of the bounds. So You have to set the frame of webview to its contentSize and take a screenshot.
ContentSize can be obtained only after the webView is loaded. So in the webviewDidFinishLoad get the height of the webview's contentsize.
CGFloat webViewHeight;
-(void)webViewDidFinishLoad:(UIWebView *)webView{
webViewHeight = webView.scrollView.contentSize.height;
}
Then set the webview height to webview contentsize as follows
webView.frame = CGRectMake(webView.frame.origin.x,webView.frame.origin.y,webView.frame.size.width,webViewHeight);
then use
UIGraphicsBeginImageContext(webview.frame.size);

Get height of UIWebView loaded content

So what I am after is a way to put a UIButton on top of a UIScrollView and a UIWebView under it. The reason I want it this way is that I need the button to "scroll away" as the user scrolls the page I load into the UIWebView.
In order for this to work, I need to get the height of the content of the web page I load and then set the height of the web view to match this. If I can do this, I then intend to set the contentSize of the UIScrollView to match the heights of the button and web view.
I understand that somehow this is supposed to happen in the - (void)webViewDidStartLoad:(UIWebView *)webView method, which is confirmed on many threads on stackoverflow (for example this one). I also know that Apple says you shouldn't put a UiWebView in a UIScrollView, since the scrolling actions may interfere. Disabling scroll for the web view should avoid the problem though.
There are many threads discussing this matter, but none of them seem to work for me. Can it be because I am running on iOS7?
I am in big desperation here, help is much appreciated!
You can get height using following code :
NSString *heightStrig = [webView stringByEvaluatingJavaScriptFromString:#"(document.height !== undefined) ? document.height : document.body.offsetHeight;"];
float height = heightStrig.floatValue;
complete code :
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGRect frame = webView.frame;
NSString *heightStrig = [webView stringByEvaluatingJavaScriptFromString:#"(document.height !== undefined) ? document.height : document.body.offsetHeight;"];
float height = heightStrig.floatValue + 10.0;
frame.size.height = height;
webView.frame = frame;
}
You can get the height of the content loaded into a UIWebView as follows:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGFloat contentHeight = webView.scrollView.contentSize.height;
}
And then adjust the frame of the web view accordingly.

Determine height of UIWebView content (embedded in UITableViewCell)

I've got a UITableView holding UIWebViews of variable height, and the user has the ability to edit the content of each web view. The content of each cell (a web view) is linked to a NSManagedObject subclass that has the height of the content stored as contentHeight. When a new set of content is initially created, it is filled in with a default set of data that has a set height, so there is no issue there.
My problem arises when the user edits the content of a web view; I need to figure out the height of the new content and update the tableview accordingly. I've already set up the logic for updating the saved contentHeight variable for the corresponding object, but I cannot find a reliable method to determine the height of the new content. Below I've listed a few of the methods I've tried and haven't been able to make work. ANY help would be greatly appreciated, I've been stumped for over an hour now :(
FAILED METHODS:
[[webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.scrollHeight"] floatValue];
[[webView stringByEvaluatingJavaScriptFromString:#"document.body.scrollHeight"] floatValue];
[[webView stringByEvaluatingJavaScriptFromString:#"document.body.offsetHeight"] floatValue];
[[webView stringByEvaluatingJavaScriptFromString:#"document.getElementById('sdcontent').offsetHeight"] floatValue];
CGRect webViewFrame = [webView frame];
CGRect temp = webViewFrame;
webViewFrame.size.height = 1;
[webView setFrame:webViewFrame];
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
webViewFrame.size = fittingSize;
[webView setFrame:temp];
it works but only when
making webview small (like 5px)
load it and wait for finish
calling sizeToFit on it then
getting the size THEN
advertisement: :D see my M42WebviewTableViewCell class on github which struggles with this

Resources