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

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

demo截屏:

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

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

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

ios epub电子书阅读器demo相关推荐

  1. ios epub电子书阅读器demo

    demo功能:读取epub电子书的阅读器demo,可以更改字体大小,查询,按电子书章节索引. demo说明:按照epub结构读取内容并显示.主要使用webview显示内容,章节内用js制作的翻页效果( ...

  2. iphone平台ePub电子书阅读器开发

    最近接到项目,开发iphone平台的ePub电子书阅读器.开始进军乔布斯iphone的开发平台.因为时间的限制,选了一个linux下开源的软件cool reader engine进行移植,因为这开源软 ...

  3. Epub电子书阅读器功能技术预研

    Epub格式演变及其文件结构: https://vernlium.github.io/2015/06/10/epub%E6%A0%BC%E5%BC%8F%E8%A7%A3%E6%9E%90/ 如何构建 ...

  4. epub电子书阅读器 EpubViewer

    桌面版epub电子文档阅读软件比较少,所以自己编写了一个EpubViwer,直接上图和功能: 功能: 可以同时打开多个Epub文件 可以多标签显示Epub电子书不同章节 左边栏显示Epub电子书目录 ...

  5. Reader电子书阅读器优化实战(一)

      我今天主要说的电子书阅读器源码如下:https://github.com/GGGHub/Reader.首先要感谢原作者的分享,有一个完整的,能运行的电子书阅读器demo.让我们为作者的这种开源精神 ...

  6. 有哪些能支持epub、txt格式的电子书阅读器?能在安卓手机上用的?

    在手机上看书有许多方便之处,随着智能手机的硬件功能越来越发达,无论大学生还是工作族每天与手机端网络资源相接触已经成为了我们生活的常态.可是不得不说手机端打开资源的方式又常常会令我们头痛,那么如何能够又 ...

  7. 有哪些能支持epub、txt格式的电子书阅读器?能在MAC上用的?

    在电脑上看书有许多方便之处,无论是使用键盘记笔记.或者是大屏幕处理网络与工作资源.确实,无论大学生还是工作族每天与电脑端网络资源相接触已经成为了我们生活的常态.可是不得不说Mac端打开资源的方式又常常 ...

  8. Delphi 10.4.2 轻松实现Android/IOS txt小说电子书阅读器应用APP翻页效果

    Delphi 10.4.2是最新版本的跨平台本机应用开发工具,一套代码可编译到五个操作系统上:iOS.Android.Windows.macOS 和 Linux: 本代码仅仅数十行即可轻松实现Andr ...

  9. 有哪些能支持epub、txt格式的电子书阅读器?在win10上用的?

    在电脑上看书有许多方便之处,无论是使用键盘记笔记.或者是大屏幕处理网络与工作资源.确实,无论大学生还是工作族每天与电脑端网络资源相接触已经成为了我们生活的常态.可是不得不说PC端打开资源的方式又常常会 ...

最新文章

  1. oracle字符界面安装,怎么解决oracle在linux 纯字符界面安装有关问题
  2. JQuery元素选择器:和||,逻辑选择
  3. C++ STL快速入门
  4. 互联网1分钟 |1210
  5. 【转载】关于如何提取Exe文件中PPT源文件的几种方法
  6. JSON.toJSONString
  7. python读书笔记2000_流畅的Python读书笔记
  8. 简单明了学习SQL CTE递归查询
  9. Tomcat最大线程数的设置
  10. dubbo绕过zookeeper直连本地提供方服务
  11. MySQL多个条件更新多个字段
  12. fscanf、fprintf的返回值
  13. c语言程序设计ns图怎么画,请各位大神帮个忙,画个NS流程图,,急!!!
  14. 扫描枪速度测试软件,条码扫描枪怎么测试
  15. 「HEOI 2014」南园满地堆轻絮
  16. 【转】ACM各种WA的说明及可能的原因
  17. 《真三国无双5》全人研究完整版
  18. matlab两曲面的交线,MATLAB里面求两相交三维曲面交线的画法
  19. 中国成语测试软件,中国汉字水平测试
  20. 竞品分析:小宇宙APP——如何在播客领域站住脚?

热门文章

  1. RK3568平台开发系列讲解(电源管理篇)RK809 电源管理芯片配置
  2. mysql下镜像安装教程_mysql的下载和安装详细教程(windows)
  3. AD18如何修改原理图页面图纸的大小
  4. 【微软Windows 7操作系统提速技巧总结】
  5. 解决时间机器无法识别硬盘问题
  6. 求生之路服务器未响应,求生之路2玩起来速度快,但是过几秒后,就卡住显示未响应,这是为什么...
  7. pycharm运行报错:Process finished with exit code -1073741515 (0xC0000135)
  8. 内容为王--分享经验、成就百万技术名博(3)
  9. 苹果8黑屏无法强制开机_iphonexr突然黑屏无法开机怎么解决?
  10. 中兴u31网管服务器,中兴通讯100G光网络网管解决方案——NetNumenTM U31(BN)