Perfect solution-adaptive content height for iOS WebView, ioswebview

Source: Internet
Author: User

Perfect solution-adaptive content height for iOS WebView, ioswebview

/// // Initialization, self. view is the parent control ///_ webView = [[UIWebView alloc] initWithFrame: CGRectMake (0, 0, self. view. frame. size. width, 0)]; _ webView. delegate = self; _ webView. scrollView. bounces = NO; _ webView. scrollView. showsHorizontalScrollIndicator = NO; _ webView. scrollView. scrollEnabled = NO; [_ webView sizeToFit];
//////////////////////////// Set the content. Here, a div is encapsulated, used to obtain the actual height (pixel) of the content. htmlcontent is an html string // NSString * htmlcontent = [NSString stringWithFormat: @ "<div id = \" webview_content_wrapper \ ">%@ </div>", htmlcontent]; [_ webView loadHTMLString: htmlcontent baseURL: nil];
//// // Method overload of delegate //// //////////////////////////////////////// -(void) webViewDidFinishLoad :( UIWebView *) webView {// obtain the page height (pixel) NSString * clientheight_str = [webView stringByEvaluatingJavaScriptFromString: @ "document. body. offsetHeight "]; float clientheight = [clientheight_str floatValue]; // set it to WebView. frame = CGRectMake (0, 0, self. view. frame. size. width, clientheight); // obtain the optimal WebView size (point) CGSize frame = [webView sizeThatFits: webView. frame. size]; // obtain the actual height (pixel) of the content NSString * height_str = [webView stringByEvaluatingJavaScriptFromString: @ "document. getElementById ('webview _ content_wrapper '). offsetHeight + parseInt (window. getComputedStyle (document. getElementsByTagName ('body') [0]). getPropertyValue ('margin-top') + parseInt (window. getComputedStyle (document. getElementsByTagName ('body') [0]). getPropertyValue ('margin-bottom ') "]; float height = [height_str floatValue]; // actual content height (pixel) * Ratio of vertices to pixels height = height * frame. height/clientheight; // set the WebView height (point) again. frame = CGRectMake (0, 0, self. view. frame. size. width, height );}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.