【问题标题】:Load HTML in UIWebview在 UIWebview 中加载 HTML
【发布时间】:2016-09-26 11:36:59
【问题描述】:

在我的应用程序中,我使用的是 UIWebview。它具有固定的高度和宽度。我需要在该 UIWebview 中加载 html 内容。

我使用了以下代码

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

但我的问题是,如果内容太小用户无法正常查看。他们需要放大它。有没有办法让内容完全适合我的网络视图。

【问题讨论】:

    标签: html ios objective-c uiwebview


    【解决方案1】:

    首先我设置滚动视图,然后在滚动视图中设置 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];
    }
    

    如果你在scrollView里面设置webView,就完美了。

    【讨论】:

      猜你喜欢
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多