Calculate height based on content in webview iOS7 - ios

Actually we are using a webview to load RTF which we get by invoking a service.
[self.messageWebView loadData:[somemessage dataUsingEncoding:NSUTF8StringEncoding]
MIMEType:#"text/rtf"
textEncodingName:#"utf-8"
baseURL:nil];
On
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGRect frame = webView.frame;
frame.size.height = 1;
frame.size.width = 1;
webView.frame = frame;
webViewSize = [webView sizeThatFits:CGSizeZero];
frame.size = webViewSize;
webView.frame = frame;
}
This is how we are calculating the height of webview based on this How to determine the content size of a UIWebView? But the size I get is not accurate. I also tried the other solution as suggested in the other post based on Scrollview, unfortunately even that solution did not work.
Can someone suggest if there is a better approach to calculate the height based on content.

Related

UiWebview in IPhone resize as per the content size

I am having an issue like i am using UIWebView in Iphone and data coming as HTML am storing the html data as astring and passing to the UIWebView am getting it in good but i want to Change the UIWebView Size as per the content size
- (void)viewDidLoad {
web.delegate = self;
NSString * str = [dict objectForKey:#"terms"];
[web loadHTMLString:str baseURL:nil];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
CGRect frame = web.frame;
frame.size.height = 1;
CGSize fittingSize = [web sizeThatFits:CGSizeZero];
frame.size = fittingSize;
web.frame = frame;
NSLog(#"size: %f, %f", fittingSize.width, fittingSize.height);
web.frame = CGRectMake(10, 100, web.scrollView.contentSize.width, web.scrollView.contentSize.height);
}
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;
}
You could use the delegate method webViewDidFinishLoad: but sometimes this is called before the HTML is fully rendered and that leads to wrong height values.
To make this work reliably you have to wait until the HTML is fully rendered and then use Javascript to send the correct height to your UIWebView.
Please have a look at this blogpost I wrote some time ago.
Do it with your web view's delegate method :
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
webView.frame = CGRectMake(0, 0, webView.scrollView.contentSize.width, webView.scrollView.contentSize.height);
}

How to find the correct uiwebview height based on the html content

I am using the following code to find the UIWebview height based on the html content, but its not returning exactly correct size. some time it give more size and some times it gives less size.I don't want to show some extra space in uiwebview
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *desccontentH = [webView stringByEvaluatingJavaScriptFromString:#"document.getElementById(\"desc\").offsetHeight;"];
NSLog(#"Webviewheight: %#", desccontentH);
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
NSLog(#"sizeThatFits:Height%f, width:%f",fittingSize.width, fittingSize.height);
.....
}
How to find the correct uiwebview height based on the html content?
Take this example used for TextView:
CGFloat descriptionContentHeight = [self sizeOfText:[NSString stringWithFormat:#"%#\n",addressTextView.text] widthOfTextView:addressTextView.frame.size.width withFont:[UIFont fontWithName:#"OpenSans-Light" size:17.5f]].height + 4;
addressTextView.contentInset = UIEdgeInsetsMake(-10,8,0,0);
[addressTextView setFrame:CGRectMake(addressTextView.frame.origin.x,addressTextView.frame.origin.y ,addressTextView.frame.size.width, descriptionContentHeight)];
-(CGSize)sizeOfText:(NSString *)textToMesure widthOfTextView:(CGFloat)width withFont:(UIFont*)font
{
CGSize size = [textToMesure sizeWithFont:font constrainedToSize:CGSizeMake(width-20.0, FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
return size;
/// FLT_MAX is definded elsewhere :)
}
Use contentSize property of webView's scrollView.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGSize size = webView.scrollView.contentSize;
NSLog(#"width = %f, height = %f", size.width, size.height);
...
}

URL is not loading completely on my UIWebView in iOS?

I am working on an iPhone application, where i am opening an URL to my UIWebView. The problem is URL is not loading perfectly. As per my requirement, i need to add UIWebView as footer view of my table. but the problem is after getting the web view content height size from webViewDidFinishLoad method, i am setting frame of my footer view. here is my code :
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
aWebView.scrollView.scrollEnabled = NO;
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;
[self setFooterView:fittingSize.height + aWebView.frame.origin.y];
}
-(void)setFooterView:(float)fittingHeight
{
bottomView.frame = CGRectMake(0, 0, 320, fittingHeight);
webViewSocial.frame = bottomView.frame;
[tableView reloadData];
}
But when i am scrolling my table view, then my UIWebView (footer view) is not scrolling completely. Can you please tell me how can i achieve my output. Thanks!
try to set like this
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:#"document.height"] floatValue];
CGFloat width = [[self.webView stringByEvaluatingJavaScriptFromString:#"document.width"] floatValue];
CGRect frame = self.webView.frame;
frame.size.height = height;
frame.size.width = width;
self.webView.frame = frame;
self.tableView.tableFooterView = webView;
}

UIScrollview imbeded within UIPageviewController resizes after page turned

So I have a UIPageviewController used that contains various news articles. The articles are presented on UIWebviews of varying lengths, and each UIWebview is contained within a UIScrollview. When the first article is loaded, everything works, and the user is able to scroll up and down the page and read the entire article. However, when the user swipes to the left or right to view the next article, the UIScrollview on that page is resized to only be the height of the screen and thus will only scroll enought to bounce back. I can tell that the UIWebview is longer than what is being shown because I have printed it's height, and when scrolling, you can see that the article continues. Also, when you swipe back to the initial article View, this UIScrollview is also resized to only be the screen size.
The UIWebView has it's content's loaded in the ArticleViewController's init method, and then is sized within the webViewDidLoad method like this:
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;
This seems to work, although tell me if you think I should change it.
To get to each article, you click on it from a UITableview presenting each of the articles. Within the DidSelectRowAtIndexPath method for this, I set up the UIPageViewController like so:
UIPageViewController *articlesPageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
articlesPageViewController.dataSource = self;
articlesPageViewController.delegate = self;
NVM! The Solution was turning off auto layout for the UIScrollview! Thanks everyone!
(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);
}
Get height of the webview then set the content size of the scrollview
OR try this way:
CGFloat height = [[myWebView stringByEvaluatingJavaScriptFromString:#"document.height"] floatValue];
CGFloat width = [[myWebView stringByEvaluatingJavaScriptFromString:#"document.width"] floatValue];
CGRect frame = myWebView.frame;
frame.size.height = height + 125.0;
frame.size.width = width;
myWebView.frame = frame;
mainScrollView.contentSize = myWebView.bounds.size;

How can i change both table view cell height and webview height based on webview content?

I have a list of 15 questions each of them again have sub questions, I'm showing these questions on webView which is present in tableViewCell. Below to this webView there are three labels and three buttons. Now problem is that I'm not getting the heights of tableViewCell based on webView's content. Please suggest me a good solution and thanks in advance.
Here is my code in webviewdidfinishload
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
int fontSize =160;
NSString *jsString = [[NSString alloc] initWithFormat:#"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", fontSize];
[aWebView stringByEvaluatingJavaScriptFromString:jsString];
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);
}

Resources