我在 UIWebview 中加载了一个 html 文件。我正在尝试在 UIWebview 中调整图像的大小并完成加载。在这个 webview 中,根据设备大小将 html 内容拆分为页数(下面的代码显示)。对于分页,我使用了滑动手势。在此之前它可以正常工作。在这个 html 文件中,我几乎没有大小不同的图像。有些图像超过了设备宽度。我需要修复图像的宽度,使其适合屏幕。我不想滚动 webview,因为我使用滑动手势进行分页。那么如何根据设备宽度重新调整图像的大小。
我使用以下代码
- (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];
}