我正在使用AePubreader示例代碼在我的應用中閱讀ePub。我面臨的問題是章節的最後一頁沒有正確渲染,而且它們離開可見區域。我以下面的方式編輯了AePubreader代碼,在ipad中以兩頁模式轉換。在兩頁模式下呈現ePub
- 在chaper.m文件webViewDiDFinishLoading有以下代碼:
- (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-left: 0px;padding-right: 0px; padding-top: 0px; padding-bottom: 0px; height: %fpx;-webkit-column-count: 2; -webkit-column-gap: 0px; ')", webView.frame.size.height];
NSString *insertRule2 = [NSString stringWithFormat:@"addCSSRule('p', 'text-align: justify;')"];
NSString *setTextSizeRule = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')",fontPercentSize];
NSString *setImageRule = [NSString stringWithFormat:@"addCSSRule('img', 'max-width: %fpx; height:%fpx;')", webView.frame.size.width-200,webView.frame.size.height-200];
NSString *setFontRule = [NSString stringWithFormat:@"addCSSRule('body', 'font-family: Arial;' , 'font-name: Arial-BoldItalicMT;')"];
[webView stringByEvaluatingJavaScriptFromString:setFontRule];
[webView stringByEvaluatingJavaScriptFromString:setImageRule];
[webView stringByEvaluatingJavaScriptFromString:varMySheet];
[webView stringByEvaluatingJavaScriptFromString:addCSSRule];
[webView stringByEvaluatingJavaScriptFromString:insertRule1];
[webView stringByEvaluatingJavaScriptFromString:insertRule2];
[webView stringByEvaluatingJavaScriptFromString:setTextSizeRule];
float totalWidth = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollWidth"] floatValue];
pageCount = (ceilf)((float)totalWidth/webView.frame.size.width);
NSLog(@"Chapter %d: %@ -> %d pages & totalWidth :: %f", chapterIndex, title, pageCount,totalWidth);
[webView dealloc];
[delegate chapterDidFinishLoad:self];
}
在EpubViewController.m webViewDidFinishLoading如下: -
- (void)webViewDidFinishLoad:(UIWebView *)theWebView{
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-left: 0px;padding-right: 0px; padding-top: 0px; padding-bottom: 0px; height: %fpx;-webkit-column-count: 2; -webkit-column-gap: 0px; ')", webView.frame.size.height];
NSString *insertRule2 = [NSString stringWithFormat:@"addCSSRule('p', 'text-align: justify;')"];
NSString *setTextSizeRule = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')", currentTextSize];
NSString *setHighlightColorRule = [NSString stringWithFormat:@"addCSSRule('highlight', 'background-color: yellow;')"];
NSString *setImageRule = [NSString stringWithFormat:@"addCSSRule('img', 'max-width: %fpx; height:%fpx;')", (webView.frame.size.width/2)-200,webView.frame.size.height-150];
NSString *setFontRule = [NSString stringWithFormat:@"addCSSRule('body', 'font-family: Arial;' , 'font-name: Arial-BoldItalicMT;')"];
[webView stringByEvaluatingJavaScriptFromString:setFontRule];
[webView stringByEvaluatingJavaScriptFromString:setImageRule];
[webView stringByEvaluatingJavaScriptFromString:varMySheet];
[webView stringByEvaluatingJavaScriptFromString:addCSSRule];
[webView stringByEvaluatingJavaScriptFromString:insertRule1];
[webView stringByEvaluatingJavaScriptFromString:insertRule2];
[webView stringByEvaluatingJavaScriptFromString:setTextSizeRule];
[webView stringByEvaluatingJavaScriptFromString:setHighlightColorRule];
if(currentSearchResult!=nil){
// NSLog(@"Highlighting %@", currentSearchResult.originatingQuery);
[webView highlightAllOccurencesOfString:currentSearchResult.originatingQuery];
}
// int totalWidth = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollWidth"] intValue];
// NSLog(@">>>>>>>>>>>>>>>>>>>>>>>total width is %d",totalWidth);
// CGFloat moduloResult = (float)(totalWidth/((int)webView.bounds.size.width % 2));
//NSLog(@"moduloresult is %f",moduloResult);
// pagesInCurrentSpineCount = (round)((float)totalWidth/(webView.frame.size.width));
float pageCountofChapter = [[loadedEpub.spineArray objectAtIndex:currentSpineIndex] pageCount];
pagesInCurrentSpineCount =(ceilf)(pageCountofChapter/2.0);
UIScrollView* sv = nil;
for (UIView* v in webView.subviews) {
if([v isKindOfClass:[UIScrollView class]])
{
sv = (UIScrollView*) v;
sv.contentSize = CGSizeMake(pagesInCurrentSpineCount*1024, sv.contentSize.height);
}
}
// NSLog(@"TotalWidth :: %d && pagesInCurrentSpineCount :: %d && currentPageInSpineIndex :: %d",totalWidth,pagesInCurrentSpineCount,currentPageInSpineIndex);
[self gotoPageInCurrentSpine:currentPageInSpineIndex];
}
我已經注意到,當計算出的頁面中奇數本章的最後一頁沒有正確渲染?另外,有沒有其他可用的庫提供兩種頁面模式支持來準確呈現epub?
謝謝先生,但最新的行動執行此方法UIScrollView * scrollV = [ self getScrollViewFromWebView:webView]; – dineshprasanna 2012-09-05 01:30:48