Setting the width and height of the UIWebView

I need to adjust the height and width of the UIWebView so that all the text is displayed in the view, and the user does not need to scroll the text to read.

Does anyone know how to get it?

+6
iphone uiwebview
source share
3 answers
webView.scalesPageToFit = YES; 

See also: UIWebView + SFHFStringMetrics

+8
source share

We hope this points you in the right direction.

stringByEvaluatingJavaScriptFromString: calling the stringByEvaluatingJavaScriptFromString: method on a UIWebView , you can execute JavaScript from your application into a UIWebView .

 // get the height of your content [webview stringByEvaluatingJavaScriptFromString:@"document.getElementById('id_of_content_container').offsetHeight;"]; // get the width of your content [webview stringByEvaluatingJavaScriptFromString:@"document.getElementById('id_of_content_container').offsetHeight;"]; 

The above returns an NSString object with the width and height of any element that you pass to the getElementById function. With this information, you can resize the UIWebView to accommodate your content. Example:

 CGRect frame = webView.frame; frame.size.width = 100; webView.frame = frame; 

You may also need to reset scalesPageToFit again so that the content is reset to the new frame size.

Another option is to detect window.height and window.width on the HTML side and use JavaScript to customize the content in accordance with aspects of the UIWebView.

+4
source share

UIWebView no special provisions for resizing content. You can set the scalesPageToFit property to YES to scale the content down, or you can make the view large enough to fit most different sizes. I come out of your question that you display content that you manage. If not, I'm not sure that there is a simple software way to achieve what you want.

+1
source share

All Articles