Resize UIWebView to its contents

In a UITextView, I can change the height of the control to its contents as follows:

CGRect frame = textView.frame; frame.size.height = textView.contentSize.height; textView.frame = frame; 

But I want to use UIWebView instead to show rich text. UIWebVie Content:

 NSString *myDescriptionHTML = [NSString 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:12], @"yadayadayada..."]; [webView loadHTMLString:myDescriptionHTML baseURL:nil]; 

Thus, the UIWebView contains only plain text. How can I achieve a result similar to the above UITextView code?

+6
source share
6 answers

Although theoretically, the JavaScript method provided

 - (void)webViewDidFinishLoad:(UIWebView *)webview CGRect oldBounds = [[self webview] bounds]; CGFloat height = [[webview stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue]; [webview setBounds:CGRectMake(oldBounds.x, oldBounds.y, oldBounds.width, height)]; } 

should work (and seems to work for most people), it's just not for me. I tried with several options and it always returned a size that has nothing to do with the true height of the content. For example, one liner will have a height of 260 or 244. I don’t know why (I would like to know).

SO, I ended up doing as hinted by this answer:

 - (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); } 

I think the JS style is more elegant, but hell, it does not work for me and cannot understand why, while this method worked out of the box.

+14
source

Similar to what Micheal suggested, you should be able to get the height of the content by accessing javascript in the webview webViewDidFinishLoad

ex (untested):

 - (void)webViewDidFinishLoad:(UIWebView *)webview CGRect oldBounds = [[self webview] bounds]; CGFloat height = [[webview stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue]; [webview setBounds:CGRectMake(oldBounds.x, oldBounds.y, oldBounds.width, height)]; } 

This is based on the method used by Appcelerator Titanium to change its webviews based on height if the user sets the height to β€œauto” https://github.com/appcelerator/titanium_mobile/blob/master/iphone/Classes/TiUIWebView. m # L616

+2
source

I know this is old, but I could not find more recent answers to my problem. I ended up with this solution. This is true when using restrictions.

 - (void)webViewDidFinishLoad:(UIWebView *)aWebView { // get content size CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero]; // ajust the height constraint of the webview to the content size [self.webviewHeightConstraint setConstant:fittingSize.height]; } 
+2
source

There is no simple answer to this question, since UIWebView is a complex view of scrolling. A few things you could try, everything should be done after the UIWebView delegate is notified that the webpage has finished loading (using webViewDidFinishLoad :). Once the delegate knows that the UIWebView is loaded, I would try this:

  • Try using the contentSize trick, it is unlikely that it will work, but maybe if the page is loaded.
  • Get the height using javascript, for example:

    CGFloat windowHeight = [[webView stringByEvaluatingJavaScriptFromString: @ "return document.body.scrollHeight;" ] floatValue];

Then do your magic with height, as with any other view.

+1
source

Is this what you are looking for? UIScrollView * scroll = [[webview subviews] objectAtIndex: 0]; CGFloat flo = scroll.contentSize.height;

+1
source

Perhaps shorten the code?

 _webView.frame = CGRectMake(_webView.frame.origin.x, _webView.frame.origin.y, _webView.frame.size.width, 1); CGSize fittingSize = [_webView sizeThatFits:CGSizeZero]; _webView.frame = CGRectMake(_webView.frame.origin.x, _webView.frame.origin.y, _webView.frame.size.width, fittingSize.height); 
0
source

Source: https://habr.com/ru/post/888872/


All Articles