ios epub电子书阅读器demo

demo功能:读取epub电子书的阅读器demo,可以更改字体大小,查询,按电子书章节索引。

demo说明:按照epub结构读取内容并显示。主要使用webview显示内容,章节内用js制作的翻页效果(效果不是很好,有点闪烁)。主要代码在:EPubViewController.m中。

demo截屏:



demo的主要代码:翻页控制部分

  1. - (void) chapterDidFinishLoad:(Chapter *)chapter{  
  2.     totalPagesCount+=chapter.pageCount;  
  3.   
  4.     if(chapter.chapterIndex + 1 < [loadedEpub.spineArray count]){  
  5.         [[loadedEpub.spineArray objectAtIndex:chapter.chapterIndex+1] setDelegate:self];  
  6.         [[loadedEpub.spineArray objectAtIndex:chapter.chapterIndex+1] loadChapterWithWindowSize:webView.bounds fontPercentSize:currentTextSize];  
  7.         [currentPageLabel setText:[NSString stringWithFormat:@"?/%d", totalPagesCount]];  
  8.     } else {  
  9.         [currentPageLabel setText:[NSString stringWithFormat:@"%d/%d",[self getGlobalPageCount], totalPagesCount]];  
  10.         [pageSlider setValue:(float)100*(float)[self getGlobalPageCount]/(float)totalPagesCount animated:YES];  
  11.         paginating = NO;  
  12.         NSLog(@"Pagination Ended!");  
  13.     }  
  14. }  
  15.   
  16. - (int) getGlobalPageCount{  
  17.     int pageCount = 0;  
  18.     for(int i=0; i<currentSpineIndex; i++){  
  19.         pageCount+= [[loadedEpub.spineArray objectAtIndex:i] pageCount];   
  20.     }  
  21.     pageCount+=currentPageInSpineIndex+1;  
  22.     return pageCount;  
  23. }  
  24.   
  25. - (void) loadSpine:(int)spineIndex atPageIndex:(int)pageIndex {  
  26.     [self loadSpine:spineIndex atPageIndex:pageIndex highlightSearchResult:nil];  
  27. }  
  28.   
  29. - (void) loadSpine:(int)spineIndex atPageIndex:(int)pageIndex highlightSearchResult:(SearchResult*)theResult{  
  30.       
  31.     webView.hidden = YES;  
  32.       
  33.     self.currentSearchResult = theResult;  
  34.   
  35.     [chaptersPopover dismissPopoverAnimated:YES];  
  36.     [searchResultsPopover dismissPopoverAnimated:YES];  
  37.       
  38.     NSURL* url = [NSURL fileURLWithPath:[[loadedEpub.spineArray objectAtIndex:spineIndex] spinePath]];  
  39.     [webView loadRequest:[NSURLRequest requestWithURL:url]];  
  40.     currentPageInSpineIndex = pageIndex;  
  41.     currentSpineIndex = spineIndex;  
  42.     if(!paginating){  
  43.         [currentPageLabel setText:[NSString stringWithFormat:@"%d/%d",[self getGlobalPageCount], totalPagesCount]];  
  44.         [pageSlider setValue:(float)100*(float)[self getGlobalPageCount]/(float)totalPagesCount animated:YES];    
  45.     }  
  46. }  
  47.   
  48. - (void) gotoPageInCurrentSpine:(int)pageIndex{  
  49.     if(pageIndex>=pagesInCurrentSpineCount){  
  50.         pageIndex = pagesInCurrentSpineCount - 1;  
  51.         currentPageInSpineIndex = pagesInCurrentSpineCount - 1;   
  52.     }  
  53.       
  54.     float pageOffset = pageIndex*webView.bounds.size.width;  
  55.   
  56. //注入js 滚动到指定的坐标--js翻页效果  
  57.     NSString* goToOffsetFunc = [NSString stringWithFormat:@" function pageScroll(xOffset){ window.scroll(xOffset,0); } "];  
  58.     NSString* goTo =[NSString stringWithFormat:@"pageScroll(%f)", pageOffset];  
  59.       
  60.     [webView stringByEvaluatingJavaScriptFromString:goToOffsetFunc];  
  61.     [webView stringByEvaluatingJavaScriptFromString:goTo];  
  62.       
  63.     if(!paginating){  
  64.         [currentPageLabel setText:[NSString stringWithFormat:@"%d/%d",[self getGlobalPageCount], totalPagesCount]];  
  65.         [pageSlider setValue:(float)100*(float)[self getGlobalPageCount]/(float)totalPagesCount animated:YES];    
  66.     }  
  67.       
  68.     webView.hidden = NO;  
  69.       
  70. }  
  71.   
  72. - (void) gotoNextSpine {  
  73.     if(!paginating){  
  74.         if(currentSpineIndex+1<[loadedEpub.spineArray count]){  
  75.             [self loadSpine:++currentSpineIndex atPageIndex:0];  
  76.         }     
  77.     }  
  78. }  
  79.   
  80. - (void) gotoPrevSpine {  
  81.     if(!paginating){  
  82.         if(currentSpineIndex-1>=0){  
  83.             [self loadSpine:--currentSpineIndex atPageIndex:0];  
  84.         }     
  85.     }  
  86. }  
  87.   
  88. - (void) gotoNextPage {  
  89.     if(!paginating){  
  90.         if(currentPageInSpineIndex+1<pagesInCurrentSpineCount){  
  91.             [self gotoPageInCurrentSpine:++currentPageInSpineIndex];  
  92.         } else {  
  93.             [self gotoNextSpine];  
  94.         }         
  95.     }  
  96. }  
  97.   
  98. - (void) gotoPrevPage {  
  99.     if (!paginating) {  
  100.         if(currentPageInSpineIndex-1>=0){  
  101.             [self gotoPageInCurrentSpine:--currentPageInSpineIndex];  
  102.         } else {  
  103.             if(currentSpineIndex!=0){  
  104.                 int targetPage = [[loadedEpub.spineArray objectAtIndex:(currentSpineIndex-1)] pageCount];  
  105.                 [self loadSpine:--currentSpineIndex atPageIndex:targetPage-1];  
  106.             }  
  107.         }  
  108.     }  
  109. }  
  110.   
  111.   
  112. - (IBAction) increaseTextSizeClicked:(id)sender{  
  113.     if(!paginating){  
  114.         if(currentTextSize+25<=200){  
  115.             currentTextSize+=25;  
  116.             [self updatePagination];  
  117.             if(currentTextSize == 200){  
  118.                 [incTextSizeButton setEnabled:NO];  
  119.             }  
  120.             [decTextSizeButton setEnabled:YES];  
  121.         }  
  122.     }  
  123. }  
  124. - (IBAction) decreaseTextSizeClicked:(id)sender{  
  125.     if(!paginating){  
  126.         if(currentTextSize-25>=50){  
  127.             currentTextSize-=25;  
  128.             [self updatePagination];  
  129.             if(currentTextSize==50){  
  130.                 [decTextSizeButton setEnabled:NO];  
  131.             }  
  132.             [incTextSizeButton setEnabled:YES];  
  133.         }  
  134.     }  
  135. }  
  136.   
  137. - (IBAction) doneClicked:(id)sender{  
  138.     [self dismissModalViewControllerAnimated:YES];  
  139. }  
  140.   
  141.   
  142. - (IBAction) slidingStarted:(id)sender{  
  143.     int targetPage = ((pageSlider.value/(float)100)*(float)totalPagesCount);  
  144.     if (targetPage==0) {  
  145.         targetPage++;  
  146.     }  
  147.     [currentPageLabel setText:[NSString stringWithFormat:@"%d/%d", targetPage, totalPagesCount]];  
  148. }  
  149.   
  150. - (IBAction) slidingEnded:(id)sender{  
  151.     int targetPage = (int)((pageSlider.value/(float)100)*(float)totalPagesCount);  
  152.     if (targetPage==0) {  
  153.         targetPage++;  
  154.     }  
  155.     int pageSum = 0;  
  156.     int chapterIndex = 0;  
  157.     int pageIndex = 0;  
  158.     for(chapterIndex=0; chapterIndex<[loadedEpub.spineArray count]; chapterIndex++){  
  159.         pageSum+=[[loadedEpub.spineArray objectAtIndex:chapterIndex] pageCount];  
  160. //      NSLog(@"Chapter %d, targetPage: %d, pageSum: %d, pageIndex: %d", chapterIndex, targetPage, pageSum, (pageSum-targetPage));  
  161.         if(pageSum>=targetPage){  
  162.             pageIndex = [[loadedEpub.spineArray objectAtIndex:chapterIndex] pageCount] - 1 - pageSum + targetPage;  
  163.             break;  
  164.         }  
  165.     }  
  166.     [self loadSpine:chapterIndex atPageIndex:pageIndex];  
  167. }  
  168.   
  169. - (IBAction) showChapterIndex:(id)sender{  
  170.     if(chaptersPopover==nil){  
  171.         ChapterListViewController* chapterListView = [[ChapterListViewController alloc] initWithNibName:@"ChapterListViewController" bundle:[NSBundle mainBundle]];  
  172.         [chapterListView setEpubViewController:self];  
  173.         chaptersPopover = [[UIPopoverController alloc] initWithContentViewController:chapterListView];  
  174.         [chaptersPopover setPopoverContentSize:CGSizeMake(400, 600)];  
  175.         [chapterListView release];  
  176.     }  
  177.     if ([chaptersPopover isPopoverVisible]) {  
  178.         [chaptersPopover dismissPopoverAnimated:YES];  
  179.     }else{  
  180.         [chaptersPopover presentPopoverFromBarButtonItem:chapterListButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];         
  181.     }  
  182. }  
  183.   
  184.   
  185. //webview 完成加载,将css注入。webview加载一次是一个epub里的xhtml内容,可能是一页,也可能是多页。  
  186. //可以看看demo中的.epub文件。将.epub改成zip 解压就可以看到epub的内容  
  187. - (void)webViewDidFinishLoad:(UIWebView *)theWebView{  
  188.       
  189.     NSString *varMySheet = @"var mySheet = document.styleSheets[0];";  
  190.       
  191.     NSString *addCSSRule =  @"function addCSSRule(selector, newRule) {"  
  192.     "if (mySheet.addRule) {"  
  193.     "mySheet.addRule(selector, newRule);"                               // For Internet Explorer  
  194.     "} else {"  
  195.     "ruleIndex = mySheet.cssRules.length;"  
  196.     "mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);"   // For Firefox, Chrome, etc.  
  197.     "}"  
  198.     "}";  
  199.       
  200.     NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", webView.frame.size.height, webView.frame.size.width];  
  201.     NSString *insertRule2 = [NSString stringWithFormat:@"addCSSRule('p', 'text-align: justify;')"];  
  202.     NSString *setTextSizeRule = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')", currentTextSize];  
  203.     NSString *setHighlightColorRule = [NSString stringWithFormat:@"addCSSRule('highlight', 'background-color: yellow;')"];  
  204.   
  205.       
  206.     [webView stringByEvaluatingJavaScriptFromString:varMySheet];  
  207.       
  208.     [webView stringByEvaluatingJavaScriptFromString:addCSSRule];  
  209.           
  210.     [webView stringByEvaluatingJavaScriptFromString:insertRule1];  
  211.       
  212.     [webView stringByEvaluatingJavaScriptFromString:insertRule2];  
  213.       
  214.     [webView stringByEvaluatingJavaScriptFromString:setTextSizeRule];  
  215.       
  216.     [webView stringByEvaluatingJavaScriptFromString:setHighlightColorRule];  
  217.       
  218.     if(currentSearchResult!=nil){  
  219.     //  NSLog(@"Highlighting %@", currentSearchResult.originatingQuery);  
  220.         [webView highlightAllOccurencesOfString:currentSearchResult.originatingQuery];  
  221.     }  
  222.       
  223.       
  224.     int totalWidth = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollWidth"] intValue];  
  225.     pagesInCurrentSpineCount = (int)((float)totalWidth/webView.bounds.size.width);  
  226.       
  227.     [self gotoPageInCurrentSpine:currentPageInSpineIndex];  
  228. }  
  229.   
  230. - (void) updatePagination{  
  231.     if(epubLoaded){  
  232.         if(!paginating){  
  233.             NSLog(@"Pagination Started!");  
  234.             paginating = YES;  
  235.             totalPagesCount=0;  
  236.             [self loadSpine:currentSpineIndex atPageIndex:currentPageInSpineIndex];  
  237.             [[loadedEpub.spineArray objectAtIndex:0] setDelegate:self];  
  238.             [[loadedEpub.spineArray objectAtIndex:0] loadChapterWithWindowSize:webView.bounds fontPercentSize:currentTextSize];  
  239.             [currentPageLabel setText:@"?/?"];  
  240.         }  
  241.     }  
  242. }  

demo下载地址: http://download.csdn.net/download/donny_zhang/5647857


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值