2016-09-26 11 views
-1

私のアプリではUIWebviewを使用しています。それは固定された高さと幅を持っています。私はそのUIWebviewでHTMLコンテンツを読み込む必要があります。HTMLをUIWebviewにロードする

私は、次のコード

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

を使用しかし、コンテンツのサイズが小さすぎる、ユーザーがそれを正しく表示できない場合は、私の問題は、あります。彼らはそれをズームする必要があります。 Webビューにコンテンツを完全に収める方法はありますか?

答えて

0

最初にスクロールビューを設定し、scrollView内にwebView.Iを設定しました。iPhone、iPadの幅と高さに応じてコンテンツサイズを設定しました。以下のコードでは、私のproject.Itでこれを試しました良い。

- (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を設定すると、完全に機能します。

関連する問題