ios epub电子书阅读器demo
demo功能:读取epub电子书的阅读器demo,可以更改字体大小,查询,按电子书章节索引。
demo说明:按照epub结构读取内容并显示。主要使用webview显示内容,章节内用js制作的翻页效果(效果不是很好,有点闪烁)。主要代码在:EPubViewController.m中。
demo截屏:
demo的主要代码:翻页控制部分
- (void) chapterDidFinishLoad:(Chapter *)chapter{totalPagesCount+=chapter.pageCount;if(chapter.chapterIndex + 1 < [loadedEpub.spineArray count]){[[loadedEpub.spineArray objectAtIndex:chapter.chapterIndex+1] setDelegate:self];[[loadedEpub.spineArray objectAtIndex:chapter.chapterIndex+1] loadChapterWithWindowSize:webView.bounds fontPercentSize:currentTextSize];[currentPageLabel setText:[NSString stringWithFormat:@"?/%d", totalPagesCount]];} else {[currentPageLabel setText:[NSString stringWithFormat:@"%d/%d",[self getGlobalPageCount], totalPagesCount]];[pageSlider setValue:(float)100*(float)[self getGlobalPageCount]/(float)totalPagesCount animated:YES];paginating = NO;NSLog(@"Pagination Ended!");}
}- (int) getGlobalPageCount{int pageCount = 0;for(int i=0; i<currentSpineIndex; i++){pageCount+= [[loadedEpub.spineArray objectAtIndex:i] pageCount]; }pageCount+=currentPageInSpineIndex+1;return pageCount;
}- (void) loadSpine:(int)spineIndex atPageIndex:(int)pageIndex {[self loadSpine:spineIndex atPageIndex:pageIndex highlightSearchResult:nil];
}- (void) loadSpine:(int)spineIndex atPageIndex:(int)pageIndex highlightSearchResult:(SearchResult*)theResult{webView.hidden = YES;self.currentSearchResult = theResult;[chaptersPopover dismissPopoverAnimated:YES];[searchResultsPopover dismissPopoverAnimated:YES];NSURL* url = [NSURL fileURLWithPath:[[loadedEpub.spineArray objectAtIndex:spineIndex] spinePath]];[webView loadRequest:[NSURLRequest requestWithURL:url]];currentPageInSpineIndex = pageIndex;currentSpineIndex = spineIndex;if(!paginating){[currentPageLabel setText:[NSString stringWithFormat:@"%d/%d",[self getGlobalPageCount], totalPagesCount]];[pageSlider setValue:(float)100*(float)[self getGlobalPageCount]/(float)totalPagesCount animated:YES]; }
}- (void) gotoPageInCurrentSpine:(int)pageIndex{if(pageIndex>=pagesInCurrentSpineCount){pageIndex = pagesInCurrentSpineCount - 1;currentPageInSpineIndex = pagesInCurrentSpineCount - 1; }float pageOffset = pageIndex*webView.bounds.size.width;//注入js 滚动到指定的坐标--js翻页效果
NSString* goToOffsetFunc = [NSString stringWithFormat:@" function pageScroll(xOffset){ window.scroll(xOffset,0); } "];NSString* goTo =[NSString stringWithFormat:@"pageScroll(%f)", pageOffset];[webView stringByEvaluatingJavaScriptFromString:goToOffsetFunc];[webView stringByEvaluatingJavaScriptFromString:goTo];if(!paginating){[currentPageLabel setText:[NSString stringWithFormat:@"%d/%d",[self getGlobalPageCount], totalPagesCount]];[pageSlider setValue:(float)100*(float)[self getGlobalPageCount]/(float)totalPagesCount animated:YES]; }webView.hidden = NO;}- (void) gotoNextSpine {if(!paginating){if(currentSpineIndex+1<[loadedEpub.spineArray count]){[self loadSpine:++currentSpineIndex atPageIndex:0];} }
}- (void) gotoPrevSpine {if(!paginating){if(currentSpineIndex-1>=0){[self loadSpine:--currentSpineIndex atPageIndex:0];} }
}- (void) gotoNextPage {if(!paginating){if(currentPageInSpineIndex+1<pagesInCurrentSpineCount){[self gotoPageInCurrentSpine:++currentPageInSpineIndex];} else {[self gotoNextSpine];} }
}- (void) gotoPrevPage {if (!paginating) {if(currentPageInSpineIndex-1>=0){[self gotoPageInCurrentSpine:--currentPageInSpineIndex];} else {if(currentSpineIndex!=0){int targetPage = [[loadedEpub.spineArray objectAtIndex:(currentSpineIndex-1)] pageCount];[self loadSpine:--currentSpineIndex atPageIndex:targetPage-1];}}}
}- (IBAction) increaseTextSizeClicked:(id)sender{if(!paginating){if(currentTextSize+25<=200){currentTextSize+=25;[self updatePagination];if(currentTextSize == 200){[incTextSizeButton setEnabled:NO];}[decTextSizeButton setEnabled:YES];}}
}
- (IBAction) decreaseTextSizeClicked:(id)sender{if(!paginating){if(currentTextSize-25>=50){currentTextSize-=25;[self updatePagination];if(currentTextSize==50){[decTextSizeButton setEnabled:NO];}[incTextSizeButton setEnabled:YES];}}
}- (IBAction) doneClicked:(id)sender{[self dismissModalViewControllerAnimated:YES];
}- (IBAction) slidingStarted:(id)sender{int targetPage = ((pageSlider.value/(float)100)*(float)totalPagesCount);if (targetPage==0) {targetPage++;}[currentPageLabel setText:[NSString stringWithFormat:@"%d/%d", targetPage, totalPagesCount]];
}- (IBAction) slidingEnded:(id)sender{int targetPage = (int)((pageSlider.value/(float)100)*(float)totalPagesCount);if (targetPage==0) {targetPage++;}int pageSum = 0;int chapterIndex = 0;int pageIndex = 0;for(chapterIndex=0; chapterIndex<[loadedEpub.spineArray count]; chapterIndex++){pageSum+=[[loadedEpub.spineArray objectAtIndex:chapterIndex] pageCount];
// NSLog(@"Chapter %d, targetPage: %d, pageSum: %d, pageIndex: %d", chapterIndex, targetPage, pageSum, (pageSum-targetPage));if(pageSum>=targetPage){pageIndex = [[loadedEpub.spineArray objectAtIndex:chapterIndex] pageCount] - 1 - pageSum + targetPage;break;}}[self loadSpine:chapterIndex atPageIndex:pageIndex];
}- (IBAction) showChapterIndex:(id)sender{if(chaptersPopover==nil){ChapterListViewController* chapterListView = [[ChapterListViewController alloc] initWithNibName:@"ChapterListViewController" bundle:[NSBundle mainBundle]];[chapterListView setEpubViewController:self];chaptersPopover = [[UIPopoverController alloc] initWithContentViewController:chapterListView];[chaptersPopover setPopoverContentSize:CGSizeMake(400, 600)];[chapterListView release];}if ([chaptersPopover isPopoverVisible]) {[chaptersPopover dismissPopoverAnimated:YES];}else{[chaptersPopover presentPopoverFromBarButtonItem:chapterListButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; }
}//webview 完成加载,将css注入。webview加载一次是一个epub里的xhtml内容,可能是一页,也可能是多页。
//可以看看demo中的.epub文件。将.epub改成zip 解压就可以看到epub的内容
- (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: 0px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", webView.frame.size.height, webView.frame.size.width];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;')"];[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];pagesInCurrentSpineCount = (int)((float)totalWidth/webView.bounds.size.width);[self gotoPageInCurrentSpine:currentPageInSpineIndex];
}- (void) updatePagination{if(epubLoaded){if(!paginating){NSLog(@"Pagination Started!");paginating = YES;totalPagesCount=0;[self loadSpine:currentSpineIndex atPageIndex:currentPageInSpineIndex];[[loadedEpub.spineArray objectAtIndex:0] setDelegate:self];[[loadedEpub.spineArray objectAtIndex:0] loadChapterWithWindowSize:webView.bounds fontPercentSize:currentTextSize];[currentPageLabel setText:@"?/?"];}}
}
demo下载地址: http://download.csdn.net/download/donny_zhang/5647857
ios epub电子书阅读器demo相关推荐
- ios epub电子书阅读器demo
demo功能:读取epub电子书的阅读器demo,可以更改字体大小,查询,按电子书章节索引. demo说明:按照epub结构读取内容并显示.主要使用webview显示内容,章节内用js制作的翻页效果( ...
- iphone平台ePub电子书阅读器开发
最近接到项目,开发iphone平台的ePub电子书阅读器.开始进军乔布斯iphone的开发平台.因为时间的限制,选了一个linux下开源的软件cool reader engine进行移植,因为这开源软 ...
- Epub电子书阅读器功能技术预研
Epub格式演变及其文件结构: https://vernlium.github.io/2015/06/10/epub%E6%A0%BC%E5%BC%8F%E8%A7%A3%E6%9E%90/ 如何构建 ...
- epub电子书阅读器 EpubViewer
桌面版epub电子文档阅读软件比较少,所以自己编写了一个EpubViwer,直接上图和功能: 功能: 可以同时打开多个Epub文件 可以多标签显示Epub电子书不同章节 左边栏显示Epub电子书目录 ...
- Reader电子书阅读器优化实战(一)
我今天主要说的电子书阅读器源码如下:https://github.com/GGGHub/Reader.首先要感谢原作者的分享,有一个完整的,能运行的电子书阅读器demo.让我们为作者的这种开源精神 ...
- 有哪些能支持epub、txt格式的电子书阅读器?能在安卓手机上用的?
在手机上看书有许多方便之处,随着智能手机的硬件功能越来越发达,无论大学生还是工作族每天与手机端网络资源相接触已经成为了我们生活的常态.可是不得不说手机端打开资源的方式又常常会令我们头痛,那么如何能够又 ...
- 有哪些能支持epub、txt格式的电子书阅读器?能在MAC上用的?
在电脑上看书有许多方便之处,无论是使用键盘记笔记.或者是大屏幕处理网络与工作资源.确实,无论大学生还是工作族每天与电脑端网络资源相接触已经成为了我们生活的常态.可是不得不说Mac端打开资源的方式又常常 ...
- Delphi 10.4.2 轻松实现Android/IOS txt小说电子书阅读器应用APP翻页效果
Delphi 10.4.2是最新版本的跨平台本机应用开发工具,一套代码可编译到五个操作系统上:iOS.Android.Windows.macOS 和 Linux: 本代码仅仅数十行即可轻松实现Andr ...
- 有哪些能支持epub、txt格式的电子书阅读器?在win10上用的?
在电脑上看书有许多方便之处,无论是使用键盘记笔记.或者是大屏幕处理网络与工作资源.确实,无论大学生还是工作族每天与电脑端网络资源相接触已经成为了我们生活的常态.可是不得不说PC端打开资源的方式又常常会 ...
最新文章
- 中国互联网+光伏建筑一体化行业商业模式创新与投资机会深度报告
- python代码基础题-python第一部分基础题1-80题
- IIS6.0下配置HTTP Gzip压缩 提高iis相应速度
- 重装系统后需要安装的软件
- tensorflow随笔-读文件
- setAutoCommit(false)导致读不到数据
- Mask-SLAM:基于语义分割掩模的鲁棒特征单目SLAM
- python矩阵后加点_Python Numpy和矩阵的相关面试问题
- Java面向对象之构造方法、构造方法重载
- vmware vcenter orchestrator configuration提示“用户名密码错误或登录失败超过次数被锁定”...
- Java 11 中 11 个不为人知的瑰宝
- Bone Collector——01背包
- 神奇的泡泡java游戏,神奇的泡泡作文400字
- 启用计算机的fn键,联想 ThinkPad 笔记本 Fn 键 关闭与启用方法
- Python 让多图排版更加美观
- 在微信小游戏中使用tensorflow的face-landmarks-detection
- VMWare下载安装以及创建虚拟机教程
- 第一回 开篇 D3D渲染流程简介
- 发卡网源码(企业和个人发卡网源码二合一)及代理系统附搭建教程
- python字典怎么增加元素_Python字典(dict)增加元素