IOS Development Solution WebView Adaptive Content Height __ios

Source: Internet
Author: User

During this time, there are a few ways to write about the content of the form that is uploaded from the backend, and then the feature that will be displayed again at the mobile end.



first of all, if the content is displayed directly, or the SizeToFit operation, then may cause the picture to exceed the screen size, the font becomes very small result, so here uses the UIWebView delegate method and adds the HTML the label language, The JavaScript action method is used. You can study the code as follows:




Web
-(UIWebView *) Detailwebview
{
    if (_detailwebview = = nil) {
        _detailwebview = [UIWebView new];
        _detailwebview.delegate = self;
        _detailwebview.scrollview.bounces = NO;
        _detailwebview.scrollview.showshorizontalscrollindicator = NO;
        _detailwebview.scrollview.scrollenabled = NO;
        _detailwebview.datadetectortypes = Uidatadetectortypeall;
        [_detailwebview SizeToFit];
    }
    return _detailwebview;
}

NSString *htmlcontent = [NSString stringwithformat:@ 

#pragma mark-----WebView delegate-(void) Webviewdidfinishload: (UIWebView *) WebView {//Get page height (pixel) nsstring *
    CLIENTHEIGHT_STR = [WebView stringbyevaluatingjavascriptfromstring: @ "Document.body.offsetHeight"];
    float clientheight = [Clientheight_str floatvalue];
    
    Set to WebView on webview.frame = CGRectMake (_wherenewslabel.bottom+10, f_device_w-30, clientheight); The following write is to obtain more accurate content height, no need to do other calculations//get content actual height (pixel) nsstring * height_str= [WebView stringbyevaluatingjavascriptfroms Tring: @ "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];
    
    Set WebView height Again (point) Webview.frame = CGRectMake (_wherenewslabel.bottom+10, f_device_w-30, height); if ([Self.delegaTe respondstoselector: @selector (backwebviewwithheight:)]) {[Self.delegate backwebviewwithheight:webview.bottom+5]
    ; }
}
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.