簡體   English   中英

在 UIWebview 中加載 HTML

[英]Load HTML in UIWebview

在我的應用程序中,我使用的是 UIWebview。 它有固定的高度和寬度。 我需要在該 UIWebview 中加載 html 內容。

我使用了以下代碼

self.contentWebView.scalePageToFit = Yes;
[self.contentWebView loadHTMLString:html baseURL:baseURL];

但我的問題是,如果內容尺寸太小,用戶無法正確查看。 他們需要放大它。 有沒有辦法讓我的網頁視圖中的內容完全適合。

首先,我設置了滾動視圖,並在 scrollView 內部設置了 webView。我根據 iPhone、iPad 的寬度和高度設置了內容大小。在下面的代碼中我提到了這一點。我為我的項目嘗試了這個。它工作正常。

- (void)webViewDidFinishLoad:(UIWebView *)webView
{

     CGFloat height = [[webview stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];
     CGFloat width = [[webview stringByEvaluatingJavaScriptFromString:@"document.width"] floatValue];
     CGRect frame = webview.frame;
     frame.size.height = height;
     frame.size.width = width;
     webview.frame = frame;

     CGRect screenBounds = [UIScreen mainScreen].bounds ;
     CGFloat widthForIpad = CGRectGetWidth(screenBounds)  ;
     CGFloat heightForIpad = CGRectGetHeight(screenBounds) ;
     NSLog(@"The iPad width is - %fl",widthForIpad);
     NSLog(@"The iPad height is - %fl",heightForIpad);

     CGFloat widthForIPhone = CGRectGetWidth(screenBounds)  ;
     CGFloat heightForIPhone = CGRectGetHeight(screenBounds) ;
     NSLog(@"The iPad width is - %fl",widthForIPhone);
     NSLog(@"The iPad height is - %fl",heightForIPhone);

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
       if ([[UIScreen mainScreen] bounds].size.height == 568) //iPhone 5s
        scrollView.contentSize = CGSizeMake(320, height+your nedded height);
       else if ([[UIScreen mainScreen] bounds].size.height == 667) ////iPhone 6,6s,7
        scrollView.contentSize = CGSizeMake(375, height+your nedded height);
       else if ([[UIScreen mainScreen] bounds].size.height == 736)//iPhone 6s plus,7 plus
        scrollView.contentSize = CGSizeMake(414, height+your nedded height);
       else{  
         //iPhone 4s
        scrollView.contentSize = CGSizeMake(320, height+your nedded height);
       }
    }

    else{
       if ([[UIScreen mainScreen] bounds].size.height == 1024){
        scrollView.contentSize = CGSizeMake(768, height+your needed height);
       }
       else{
       scrollView.contentSize = CGSizeMake(1024, height+your needed 
       }
    }
    int fontSize = 80;
    NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", fontSize];
    [webview stringByEvaluatingJavaScriptFromString:jsString];
    NSString *jsWebViewFontFamilyString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.fontFamily = 'Helvetica'"];
   [webview stringByEvaluatingJavaScriptFromString:jsWebViewFontFamilyString];
}

如果將 webView 設置在 scrollView 中,它可以完美運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM