100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > ios epub电子书阅读器demo

ios epub电子书阅读器demo

时间:2021-01-05 00:23:50

相关推荐

ios epub电子书阅读器demo

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

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

demo截屏:

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

[csharp]view plaincopyprint? -(void)chapterDidFinishLoad:(Chapter*)chapter{totalPagesCount+=chapter.pageCount;if(chapter.chapterIndex+1<[loadedEpub.spineArraycount]){[[loadedEpub.spineArrayobjectAtIndex:chapter.chapterIndex+1]setDelegate:self];[[loadedEpub.spineArrayobjectAtIndex:chapter.chapterIndex+1]loadChapterWithWindowSize:webView.boundsfontPercentSize:currentTextSize];[currentPageLabelsetText:[NSStringstringWithFormat:@"?/%d",totalPagesCount]];}else{[currentPageLabelsetText:[NSStringstringWithFormat:@"%d/%d",[selfgetGlobalPageCount],totalPagesCount]];[pageSlidersetValue:(float)100*(float)[selfgetGlobalPageCount]/(float)totalPagesCountanimated:YES];paginating=NO;NSLog(@"PaginationEnded!");}}-(int)getGlobalPageCount{intpageCount=0;for(inti=0;i<currentSpineIndex;i++){pageCount+=[[loadedEpub.spineArrayobjectAtIndex:i]pageCount];}pageCount+=currentPageInSpineIndex+1;returnpageCount;}-(void)loadSpine:(int)spineIndexatPageIndex:(int)pageIndex{[selfloadSpine:spineIndexatPageIndex:pageIndexhighlightSearchResult:nil];}-(void)loadSpine:(int)spineIndexatPageIndex:(int)pageIndexhighlightSearchResult:(SearchResult*)theResult{webView.hidden=YES;self.currentSearchResult=theResult;[chaptersPopoverdismissPopoverAnimated:YES];[searchResultsPopoverdismissPopoverAnimated:YES];NSURL*url=[NSURLfileURLWithPath:[[loadedEpub.spineArrayobjectAtIndex:spineIndex]spinePath]];[webViewloadRequest:[NSURLRequestrequestWithURL:url]];currentPageInSpineIndex=pageIndex;currentSpineIndex=spineIndex;if(!paginating){[currentPageLabelsetText:[NSStringstringWithFormat:@"%d/%d",[selfgetGlobalPageCount],totalPagesCount]];[pageSlidersetValue:(float)100*(float)[selfgetGlobalPageCount]/(float)totalPagesCountanimated:YES];}}-(void)gotoPageInCurrentSpine:(int)pageIndex{if(pageIndex>=pagesInCurrentSpineCount){pageIndex=pagesInCurrentSpineCount-1;currentPageInSpineIndex=pagesInCurrentSpineCount-1;}floatpageOffset=pageIndex*webView.bounds.size.width;//注入js滚动到指定的坐标--js翻页效果NSString*goToOffsetFunc=[NSStringstringWithFormat:@"functionpageScroll(xOffset){window.scroll(xOffset,0);}"];NSString*goTo=[NSStringstringWithFormat:@"pageScroll(%f)",pageOffset];[webViewstringByEvaluatingJavaScriptFromString:goToOffsetFunc];[webViewstringByEvaluatingJavaScriptFromString:goTo];if(!paginating){[currentPageLabelsetText:[NSStringstringWithFormat:@"%d/%d",[selfgetGlobalPageCount],totalPagesCount]];[pageSlidersetValue:(float)100*(float)[selfgetGlobalPageCount]/(float)totalPagesCountanimated:YES];}webView.hidden=NO;}-(void)gotoNextSpine{if(!paginating){if(currentSpineIndex+1<[loadedEpub.spineArraycount]){[selfloadSpine:++currentSpineIndexatPageIndex:0];}}}-(void)gotoPrevSpine{if(!paginating){if(currentSpineIndex-1>=0){[selfloadSpine:--currentSpineIndexatPageIndex:0];}}}-(void)gotoNextPage{if(!paginating){if(currentPageInSpineIndex+1<pagesInCurrentSpineCount){[selfgotoPageInCurrentSpine:++currentPageInSpineIndex];}else{[selfgotoNextSpine];}}}-(void)gotoPrevPage{if(!paginating){if(currentPageInSpineIndex-1>=0){[selfgotoPageInCurrentSpine:--currentPageInSpineIndex];}else{if(currentSpineIndex!=0){inttargetPage=[[loadedEpub.spineArrayobjectAtIndex:(currentSpineIndex-1)]pageCount];[selfloadSpine:--currentSpineIndexatPageIndex:targetPage-1];}}}}-(IBAction)increaseTextSizeClicked:(id)sender{if(!paginating){if(currentTextSize+25<=200){currentTextSize+=25;[selfupdatePagination];if(currentTextSize==200){[incTextSizeButtonsetEnabled:NO];}[decTextSizeButtonsetEnabled:YES];}}}-(IBAction)decreaseTextSizeClicked:(id)sender{if(!paginating){if(currentTextSize-25>=50){currentTextSize-=25;[selfupdatePagination];if(currentTextSize==50){[decTextSizeButtonsetEnabled:NO];}[incTextSizeButtonsetEnabled:YES];}}}-(IBAction)doneClicked:(id)sender{[selfdismissModalViewControllerAnimated:YES];}-(IBAction)slidingStarted:(id)sender{inttargetPage=((pageSlider.value/(float)100)*(float)totalPagesCount);if(targetPage==0){targetPage++;}[currentPageLabelsetText:[NSStringstringWithFormat:@"%d/%d",targetPage,totalPagesCount]];}-(IBAction)slidingEnded:(id)sender{inttargetPage=(int)((pageSlider.value/(float)100)*(float)totalPagesCount);if(targetPage==0){targetPage++;}intpageSum=0;intchapterIndex=0;intpageIndex=0;for(chapterIndex=0;chapterIndex<[loadedEpub.spineArraycount];chapterIndex++){pageSum+=[[loadedEpub.spineArrayobjectAtIndex:chapterIndex]pageCount];//NSLog(@"Chapter%d,targetPage:%d,pageSum:%d,pageIndex:%d",chapterIndex,targetPage,pageSum,(pageSum-targetPage));if(pageSum>=targetPage){pageIndex=[[loadedEpub.spineArrayobjectAtIndex:chapterIndex]pageCount]-1-pageSum+targetPage;break;}}[selfloadSpine:chapterIndexatPageIndex:pageIndex];}-(IBAction)showChapterIndex:(id)sender{if(chaptersPopover==nil){ChapterListViewController*chapterListView=[[ChapterListViewControlleralloc]initWithNibName:@"ChapterListViewController"bundle:[NSBundlemainBundle]];[chapterListViewsetEpubViewController:self];chaptersPopover=[[UIPopoverControlleralloc]initWithContentViewController:chapterListView];[chaptersPopoversetPopoverContentSize:CGSizeMake(400,600)];[chapterListViewrelease];}if([chaptersPopoverisPopoverVisible]){[chaptersPopoverdismissPopoverAnimated:YES];}else{[chaptersPopoverpresentPopoverFromBarButtonItem:chapterListButtonpermittedArrowDirections:UIPopoverArrowDirectionAnyanimated:YES];}}//webview完成加载,将css注入。webview加载一次是一个epub里的xhtml内容,可能是一页,也可能是多页。//可以看看demo中的.epub文件。将.epub改成zip解压就可以看到epub的内容-(void)webViewDidFinishLoad:(UIWebView*)theWebView{NSString*varMySheet=@"varmySheet=document.styleSheets[0];";NSString*addCSSRule=@"functionaddCSSRule(selector,newRule){""if(mySheet.addRule){""mySheet.addRule(selector,newRule);"//ForInternetExplorer"}else{""ruleIndex=mySheet.cssRules.length;""mySheet.insertRule(selector+'{'+newRule+';}',ruleIndex);"//ForFirefox,Chrome,etc."}""}";NSString*insertRule1=[NSStringstringWithFormat:@"addCSSRule('html','padding:0px;height:%fpx;-webkit-column-gap:0px;-webkit-column-width:%fpx;')",webView.frame.size.height,webView.frame.size.width];NSString*insertRule2=[NSStringstringWithFormat:@"addCSSRule('p','text-align:justify;')"];NSString*setTextSizeRule=[NSStringstringWithFormat:@"addCSSRule('body','-webkit-text-size-adjust:%d%%;')",currentTextSize];NSString*setHighlightColorRule=[NSStringstringWithFormat:@"addCSSRule('highlight','background-color:yellow;')"];[webViewstringByEvaluatingJavaScriptFromString:varMySheet];[webViewstringByEvaluatingJavaScriptFromString:addCSSRule];[webViewstringByEvaluatingJavaScriptFromString:insertRule1];[webViewstringByEvaluatingJavaScriptFromString:insertRule2];[webViewstringByEvaluatingJavaScriptFromString:setTextSizeRule];[webViewstringByEvaluatingJavaScriptFromString:setHighlightColorRule];if(currentSearchResult!=nil){//NSLog(@"Highlighting%@",currentSearchResult.originatingQuery);[webViewhighlightAllOccurencesOfString:currentSearchResult.originatingQuery];}inttotalWidth=[[webViewstringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollWidth"]intValue];pagesInCurrentSpineCount=(int)((float)totalWidth/webView.bounds.size.width);[selfgotoPageInCurrentSpine:currentPageInSpineIndex];}-(void)updatePagination{if(epubLoaded){if(!paginating){NSLog(@"PaginationStarted!");paginating=YES;totalPagesCount=0;[selfloadSpine:currentSpineIndexatPageIndex:currentPageInSpineIndex];[[loadedEpub.spineArrayobjectAtIndex:0]setDelegate:self];[[loadedEpub.spineArrayobjectAtIndex:0]loadChapterWithWindowSize:webView.boundsfontPercentSize:currentTextSize];[currentPageLabelsetText:@"?/?"];}}}

demo下载地址:/download/donny_zhang/5647857

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。