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端打开资源的方式又常常会 ...
最新文章
- oracle字符界面安装,怎么解决oracle在linux 纯字符界面安装有关问题
- JQuery元素选择器:和||,逻辑选择
- C++ STL快速入门
- 互联网1分钟 |1210
- 【转载】关于如何提取Exe文件中PPT源文件的几种方法
- JSON.toJSONString
- python读书笔记2000_流畅的Python读书笔记
- 简单明了学习SQL CTE递归查询
- Tomcat最大线程数的设置
- dubbo绕过zookeeper直连本地提供方服务
- MySQL多个条件更新多个字段
- fscanf、fprintf的返回值
- c语言程序设计ns图怎么画,请各位大神帮个忙,画个NS流程图,,急!!!
- 扫描枪速度测试软件,条码扫描枪怎么测试
- 「HEOI 2014」南园满地堆轻絮
- 【转】ACM各种WA的说明及可能的原因
- 《真三国无双5》全人研究完整版
- matlab两曲面的交线,MATLAB里面求两相交三维曲面交线的画法
- 中国成语测试软件,中国汉字水平测试
- 竞品分析:小宇宙APP——如何在播客领域站住脚?
热门文章
- RK3568平台开发系列讲解(电源管理篇)RK809 电源管理芯片配置
- mysql下镜像安装教程_mysql的下载和安装详细教程(windows)
- AD18如何修改原理图页面图纸的大小
- 【微软Windows 7操作系统提速技巧总结】
- 解决时间机器无法识别硬盘问题
- 求生之路服务器未响应,求生之路2玩起来速度快,但是过几秒后,就卡住显示未响应,这是为什么...
- pycharm运行报错:Process finished with exit code -1073741515 (0xC0000135)
- 内容为王--分享经验、成就百万技术名博(3)
- 苹果8黑屏无法强制开机_iphonexr突然黑屏无法开机怎么解决?
- 中兴u31网管服务器,中兴通讯100G光网络网管解决方案——NetNumenTM U31(BN)