2012-07-06 35 views
3

我加载一个HTML文件在UIWebview.I我试图调整UIWebview中的图像大小加载。在此web视图中,根据设备大小将html内容拆分为多个页面(下面的代码显示)。对于我使用的滑动手势分页。它正常工作,直到this.In这个html文件我有几个图像的大小互不相同。有些图像不仅仅是设备的宽度。我需要修正图像的宽度,使其适合屏幕。我不想滚动webview,因为我使用了滑动手势进行分页。那么,如何根据设备宽度重新调整图像的大小。如何在UIWebview中调整图像大小?

我用下面的代码

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


    NSString *varMySheet = @"var mySheet = document.styleSheets[0];"; 

    NSString *addCSSRule = @"function addCSSRule(selector, newRule) {" 
    "if (mySheet.addRule) {" 
    "mySheet.addRule(selector, newRule);"        // For Internet Explorer 
    "} else {" 
    "ruleIndex = mySheet.cssRules.length;" 
    "mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);" // For Firefox, Chrome, etc. 
    "}" 
    "}"; 

    NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", webview4epub.frame.size.height, webview4epub.frame.size.width]; 
    NSString *insertRule2 = [NSString stringWithFormat:@"addCSSRule('p', 'text-align: justify;')"]; 

    [webview4epub stringByEvaluatingJavaScriptFromString:varMySheet]; 
    [webview4epub stringByEvaluatingJavaScriptFromString:addCSSRule]; 
    [webview4epub stringByEvaluatingJavaScriptFromString:insertRule1]; 
    [webview4epub stringByEvaluatingJavaScriptFromString:insertRule2]; 
    NSString* foundHits = [webview4epub stringByEvaluatingJavaScriptFromString:@"results"]; 
    NSMutableArray* objects = [[NSMutableArray alloc] init]; 

    NSArray* stringObjects = [foundHits componentsSeparatedByString:@";"]; 
    for(int i=0; i<[stringObjects count]; i++){ 
     NSArray* strObj = [[stringObjects objectAtIndex:i] componentsSeparatedByString:@","]; 
     if([strObj count]==3){ 
      [objects addObject:strObj]; 
      } 
     } 


    int totalWidth = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollWidth"] intValue]; 

} 

回答

0

此大小的头图像以90%的网页视图高度:

NSString *imgHeightJSString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('img')[0].style.maxHeight = '%fpx'", webView.bounds.size.height*0.90]; 
[webView stringByEvaluatingJavaScriptFromString:imgHeightJSString]; 

这90%IMAGESIZE无论什么view.size:

[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('img')[0].style.maxHeight = '90%%'"] ; 

设置widht只需使用maxWidth而不是maxHeight。