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);
}
Related
I am trying to make a UIWebView with "dynamic screen height". This webview is inside a UIScrollView, below other components. See the layout (it uses autolayout):
My idea is to provide just one scroll (from UIScrollView - its working) and make the WebView viewport grow depending the content size. And its not working.
What I did to try to do this:
UIWebView property "scale page to fit " is on;
UIWebView is unpaginated;
UIWebView does not allow user interaction.
In my UIViewController:
- (void) viewDidAppear:(BOOL)animated {
NSString *urlString = #"MY URL GOES HERE";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[_webViewDescription loadRequest:urlRequest];
_webViewDescription.delegate = self;
_webViewDescription.scrollView.scrollEnabled = NO;
_webViewDescription.scrollView.bounces = NO;
}
My UIWebViewDelegate:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGRect frame = webView.frame;
frame.size.height = 1;
webView.frame = frame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
webView.frame = frame;
NSLog(#"size: %f, %f", fittingSize.width, fittingSize.height);
_myScrollView.contentSize = webView.bounds.size;
}
When I run the code, it prints the correct UIScrollView size and UIWebView size. The UIScrollView height got bigger, but the UIWebView maintains the same height from the first loading.
I am testing in a real device.
add a height constraint to your webView and make a IBOutlet of that like
#property(strong, nonatomic) IBOutlet NSLayoutConstraint *webViewHeightConstraint;
load your webview and in web view delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
get webview content height and set webViewHeightConstraint.constant
like below:-
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *str = [webView stringByEvaluatingJavaScriptFromString:#"(document.height !== undefined) ? document.height : document.body.offsetHeight;"];
CGFloat height = str.floatValue;
webViewHeightConstraint.constant = height;
}
hope it may help you.
Try to set webView.frame = frame; in viewDidLayoutSubviews()
For setting dynamic height for UIWebView, you need to set the height in webViewDidFinishLoadView method
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGFloat height = [[webview stringByEvaluatingJavaScriptFromString:#"document.height"] floatValue];
//For Width
//CGFloat width = [[webview stringByEvaluatingJavaScriptFromString:#"document.width"] floatValue];
CGRect frame = webview1.frame;
frame.size.height = height;
//frame.size.width = width; //Width
webview.frame = frame;
CGRect screenBounds = [UIScreen mainScreen].bounds ;
CGFloat widthForIpad = CGRectGetWidth(screenBounds) ;
CGFloat heightForIpad = CGRectGetHeight(screenBounds) ;
NSLog(#"The iPad width is - %fl",widthForIpad);
NSLog(#"The iPad height is - %fl",heightForIpad);
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
if ([[UIScreen mainScreen] bounds].size.height == 568)
scrollView.contentSize = CGSizeMake(320, height+370); //set your required height
else
scrollView.contentSize = CGSizeMake(320, height+350); //set your required height
}
else
{
if ([[UIScreen mainScreen] bounds].size.height == 1024)
scrollView.contentSize = CGSizeMake(320, height+370); //For iPad
}
}
You can get the content size of webview content using webView.scrollView.contentSize.height.
and than you can use this webView.scrollView.contentSize.height to set the contentSize of the scrollview inside webViewDidFinishLoad method.
Like this in webViewDidFinishLoad method
[objWebView setFrame:CGRectMake(objWebView.frame.origin.x, objWebView.frame.origin.y, objWebView.frame.size.width, webView.scrollView.contentSize.height)];
if(objWebView.frame.origin.y+objWebView.frame.size.height > objScrollView.contentSize.height) {
[objScrollView setContentSize:CGSizeMake(objScrollView.frame.size.width, objWebView.frame.origin.y+objWebView.frame.size.height)];
}
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);
...
}
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;
}
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);
}
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.