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

ios epub电子书阅读器demo

时间:2018-09-28 00:09:10

相关推荐

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下载地址:/download/donny_zhang/5647857

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