I'm trying to use UIWebView inside UIScrollView, I want to display an webview under an image and make them scrollable.
So I flowed those step :
Desactivate the Scroll Property for UIWebView
Set the UIScrollView and UIWebView height equals to content size.
And this my code :
- (void)viewDidLoad {
[super viewDidLoad];
self.contentReader.scrollView.scrollEnabled = NO;
NSString *myDescriptionHTML = [NSString stringWithFormat:#"<html> \n"
"<head> \n"
"<style type=\"text/css\"> \n"
"body {font-family: \"%#\"; font-size: %d;}\n"
"</style> \n"
"</head> \n"
"<body>%#</body> \n"
"</html>", #"JF Flat", 18, self.thePost.content];
[self.contentReader loadHTMLString:[NSString stringWithFormat:#"<div style='text-align:right; text-align:justify; direction:rtl'><style type='text/css'>img { max-width: 100%%; width: auto; height: auto; }</style><style type='text/css'>iframe { max-width: 100%%; width: auto; height: auto; }</style>%#<div>",myDescriptionHTML] baseURL:nil];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
// get the html content height
NSString *output = [webView stringByEvaluatingJavaScriptFromString:#"document.height;"];
NSLog(#"Content Size %#",output);
// adjust the height of webView
NSLog(#"WebView Hieght before Update%f", webView.frame.size.height);
CGRect frame = webView.frame;
frame.size.height = 1;
webView.frame = frame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
webView.frame = frame;
NSLog(#"WebView Hieght After Update%f", webView.frame.size.height);
[self.scroller setContentSize:webView.bounds.size];
}
And This is the Log message :
[5150:203791] WebView Hieght before Update360.000000
[5150:203791] WebView Hieght After Update5888.000000
The Problem is that when I scroll down the content is not showed, nothing is showed. check the picture :
Update
Debug View Hearachy.
As you can see the WebView height in the view hierarchy hasn't changed. Only the web page has changed the size.
We already solved it in the comments, so let's just put this answer for completeness' sake.
If you're using Auto Layout, make sure to change the size of the WebView by setting constraints such as a height constraint and changing its constant to make the layout change. If you want to animate a specific Auto Layout change, do something like this:
[UIView animateWithDuration:0.5 animations:^{
self.webViewHeightConstraint.constant = fittingSize.height;
[self.view layoutIfNeeded];
}];
Kind of a long shot but you might want to try
webView.scrollView.contentSize = fittingSize;
or
webView.scrollView.frame = CGRectMake(0,0,fittingSize.width, fittingSize.height);
Because scrolling was turned off the content size or frame may not be changing after loading the html. Like I said a long shot but might work.
Related
I have taken a web view and load html like this
NSMutableString *myDescriptionHTML = [NSMutableString stringWithFormat:#"<html> \n"
"<head> \n"
"<style type=\"text/css\"> \n"
"body {font-family: \"%#\"; font-size: %#;}\n"
"</style> \n"
"</head> \n"
"<body>%#</body> \n"
"</html>", #"helvetica", [NSNumber numberWithInt:40], messageModel.content];
i want to set web view height to web view content size height after web view loaded html content
Tried ways
1)
CGRect frame = self.webViewMain.frame;
frame.size.height = 1;
self.webViewMain.frame = frame;
CGSize fittingSize = [self.webViewMain sizeThatFits:CGSizeZero];
frame.size = fittingSize;
self.webViewMain.frame = frame;
Height = fittingSize.height;
NSLog(#"size: %f, %f", fittingSize.width, fittingSize.height);
[tableViewMain reloadData];
its too long height
2)
Height = [[self.webViewMain stringByEvaluatingJavaScriptFromString:#"document.documentElement.scrollHeight;"] floatValue];
3)
Heiht = self.webViewMain.scrollView.contentSize.Height
But no one is actual height
help me thanks....
UIWebview is a subclass of UIScrollview so you can set Webview.content size using CGSize. That will set your web view content height and width as required.
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);
}
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'm using a UIWebView for displaying content, in the form of an HTML string – not a website, higher than the screen of the iPhone, without needing to scroll in the webView itself, leaving that to the parent scrollView.
To achieve this, I need a way to get the total document size, including the scrollable area, to set the webView's height. I have tried a number of different Javascript solutions:
(document.height !== undefined) ? document.height : document.body.offsetHeight // Returns height of UIWebView
document.body.offsetHeight // Returns zero
document.body.clientHeight // Returns zero
document.documentElement.clientHeight // Returns height of UIWebView
window.innerHeight // Returns height of UIWebView -2
document.body.scrollHeight // Returns zero
Is there a solution that actually works?
Current (nonworking) code:
[[[self.singlePost.contentText subviews] lastObject] setScrollEnabled:NO];
int content_height = [[self.singlePost.contentText stringByEvaluatingJavaScriptFromString: #"document.body.offsetHeight"] intValue];
NSLog(#"Content_height: %d", content_height);
CGRect rect = self.singlePost.contentText.frame;
rect.size.height = content_height;
self.singlePost.contentText.frame = rect;
There is no need to use Javascript in iOS 5.0 and up - you have direct, documented access to its scrollView:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGFloat contentHeight = webView.scrollView.contentSize.height;
// ....
}
To get the total height of the contents of the webView.
When I tried with my code, I found,
(1) NSLog(#"webView height: %#", [webView stringByEvaluatingJavaScriptFromString: #"document.body.offsetHeight"]);
(2) NSLog(#"webView height: %#", [webView stringByEvaluatingJavaScriptFromString: #"document.height"]);
(1) return the height less than the original height. But (2) return the actual height. My suggestion is to get the Max of the two.
- (void)webViewDidFinishLoad:(UIWebView *)theWebView {
NSLog(#"Body height: %#", [webView stringByEvaluatingJavaScriptFromString: #"document.body.offsetHeight"]);
NSLog(#"Doc height: %#", [webView stringByEvaluatingJavaScriptFromString: #"document.height"]);
NSString * javaScript = [NSString stringWithFormat:#"document.getElementById('%#').clientHeight", kDivID];
NSLog(#"Div height: %#",[webView stringByEvaluatingJavaScriptFromString:javaScript]);
[self reLayout];
}
- (CGFloat) getWebViewPageHeight {
CGFloat height1 = [[webView stringByEvaluatingJavaScriptFromString: #"document.height"] floatValue];
CGFloat height2 = [[webView stringByEvaluatingJavaScriptFromString: #"document.body.offsetHeight"] floatValue];
return MAX(height1, height2);
}
Output
Body height: 485
Doc height: 509
Div height: 485
Where do you call your code?
For me it returns 0 if it is called right after the loadHTMLString Method.
If I call it in the (void)webViewDidFinishLoad:(UIWebView *)theWebView delegate, I get a valid value.
- (void)loadHTML: (NSString *)html {
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
webView.delegate = self;
NSURL *resourceURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[webView loadHTMLString:html baseURL: resourceURL];
NSLog(#"height: %d", [[webView stringByEvaluatingJavaScriptFromString: #"document.body.offsetHeight"] intValue]); // returns 0
}
- (void)webViewDidFinishLoad:(UIWebView *)theWebView {
NSLog(#"height: %#", [webView stringByEvaluatingJavaScriptFromString: #"document.body.offsetHeight"]); //return 2166
}
I went with a slightly different approach, to create a <DIV> that I could key off of to get the size, because I was having no luck with inspecting document/body.
This is MonoTouch C#, but should work fine in Objective-C:
private const string DIV_ID = "_uiwebview_";
LoadHtmlString("<body style='background-color:#yourcolor; padding:0px; margin:0px'>" +
"<div style='width:" + Frame.Width + "px; padding:0px; margin:0px; font-family:" +
font.Name + "' id=" + DIV_ID + ">" + html + "</div></body>", base_url);
and getting the height in LoadFinished:
string sheight = EvaluateJavascript("document.getElementById('" + DIV_ID +
"').clientHeight");
#Jesse, this works when updating with HTML that would generate a smaller size than was previously shown.
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.