2012-08-06 3 views
3

내 앱에서 ePub를 읽으려면 AePubreader 샘플 코드를 사용하고 있습니다. 내가 직면 한 문제는 장의 마지막 페이지가 제대로 렌더링되지 않고 보이는 영역을 벗어나는 것입니다. 나는 ipad에서 2 페이지 모드로 변환하기 위해 다음과 같이 AePubreader 코드를 편집했다.두 페이지 모드로 ePub 렌더링

-In chaper.m의 파일 webViewDiDFinishLoading이 다음 한 코드를 다음과 같이 EpubViewController.m webViewDidFinishLoading에서

- (void) webViewDidFinishLoad:(UIWebView*)webView{ 
    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-left: 0px;padding-right: 0px; padding-top: 0px; padding-bottom: 0px; height: %fpx;-webkit-column-count: 2; -webkit-column-gap: 0px; ')", webView.frame.size.height];  

    NSString *insertRule2 = [NSString stringWithFormat:@"addCSSRule('p', 'text-align: justify;')"]; 
    NSString *setTextSizeRule = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')",fontPercentSize]; 

    NSString *setImageRule = [NSString stringWithFormat:@"addCSSRule('img', 'max-width: %fpx; height:%fpx;')", webView.frame.size.width-200,webView.frame.size.height-200]; 


    NSString *setFontRule = [NSString stringWithFormat:@"addCSSRule('body', 'font-family: Arial;' , 'font-name: Arial-BoldItalicMT;')"]; 

    [webView stringByEvaluatingJavaScriptFromString:setFontRule]; 
    [webView stringByEvaluatingJavaScriptFromString:setImageRule]; 

    [webView stringByEvaluatingJavaScriptFromString:varMySheet]; 

    [webView stringByEvaluatingJavaScriptFromString:addCSSRule]; 

    [webView stringByEvaluatingJavaScriptFromString:insertRule1]; 

    [webView stringByEvaluatingJavaScriptFromString:insertRule2]; 

    [webView stringByEvaluatingJavaScriptFromString:setTextSizeRule]; 

    float totalWidth = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollWidth"] floatValue]; 
    pageCount = (ceilf)((float)totalWidth/webView.frame.size.width); 

    NSLog(@"Chapter %d: %@ -> %d pages & totalWidth :: %f", chapterIndex, title, pageCount,totalWidth); 

    [webView dealloc]; 
    [delegate chapterDidFinishLoad:self]; 

} 

은 다음과 같습니다 -

- (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-left: 0px;padding-right: 0px; padding-top: 0px; padding-bottom: 0px; height: %fpx;-webkit-column-count: 2; -webkit-column-gap: 0px; ')", webView.frame.size.height];  


    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;')"]; 

    NSString *setImageRule = [NSString stringWithFormat:@"addCSSRule('img', 'max-width: %fpx; height:%fpx;')", (webView.frame.size.width/2)-200,webView.frame.size.height-150]; 

    NSString *setFontRule = [NSString stringWithFormat:@"addCSSRule('body', 'font-family: Arial;' , 'font-name: Arial-BoldItalicMT;')"]; 

    [webView stringByEvaluatingJavaScriptFromString:setFontRule]; 

    [webView stringByEvaluatingJavaScriptFromString:setImageRule]; 

    [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]; 

// NSLog(@">>>>>>>>>>>>>>>>>>>>>>>total width is %d",totalWidth); 
// CGFloat moduloResult = (float)(totalWidth/((int)webView.bounds.size.width % 2)); 

    //NSLog(@"moduloresult is %f",moduloResult); 

// pagesInCurrentSpineCount = (round)((float)totalWidth/(webView.frame.size.width)); 
    float pageCountofChapter = [[loadedEpub.spineArray objectAtIndex:currentSpineIndex] pageCount]; 
    pagesInCurrentSpineCount =(ceilf)(pageCountofChapter/2.0); 

    UIScrollView* sv = nil; 
    for (UIView* v in webView.subviews) { 
     if([v isKindOfClass:[UIScrollView class]]) 
     { 
      sv = (UIScrollView*) v; 
      sv.contentSize = CGSizeMake(pagesInCurrentSpineCount*1024, sv.contentSize.height); 
     } 
    } 

// NSLog(@"TotalWidth :: %d && pagesInCurrentSpineCount :: %d && currentPageInSpineIndex :: %d",totalWidth,pagesInCurrentSpineCount,currentPageInSpineIndex); 

    [self gotoPageInCurrentSpine:currentPageInSpineIndex]; 
} 

내가 발견 한 그 계산 된 페이지에있을 때 홀수 번째 장의 마지막 페이지가 제대로 렌더링되지 않습니다? 또한, epub을 정확하게 렌더링하기 위해 두 페이지 모드 지원을 제공하는 대체 라이브러리가 있습니까?

답변

1

@dineshprasanna 다음과 같이 프로젝트를 변경하십시오. 당신의 epubviewcontroller 파일에 chapter.m 파일

- (void) webViewDidFinishLoad:(UIWebView*)webView{ 
    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. 
    "}" 
    "}"; 

// NSLog(@"w:%f h:%f", webView.bounds.size.width, webView.bounds.size.height); 



    NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding-left: 50px;padding-right: 50px; padding-top: 50px; padding-bottom: 50px; -webkit-column-gap: 100px;height: %fpx; -webkit-column-width : %fpx')", webView.frame.size.height-100,webView.frame.size.width/2-200];  

    NSString *insertRule2 = [NSString stringWithFormat:@"addCSSRule('p', 'text-align: justify;')"]; 
    NSString *setTextSizeRule = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')",fontPercentSize]; 

// NSString *setImageRule = [NSString stringWithFormat:@"addCSSRule('img', 'max-width: %fpx; height:auto;')", webView.frame.size.width *0.75]; 
    NSString *setImageRule = [NSString stringWithFormat:@"addCSSRule('img', 'max-width: %fpx; height:%fpx;')", webView.frame.size.width/2-200,webView.frame.size.height-200]; 


    NSString *setFontRule = [NSString stringWithFormat:@"addCSSRule('body', 'font-family: Arial;' , 'font-name: Arial-BoldItalicMT;')"]; 

    [webView stringByEvaluatingJavaScriptFromString:setFontRule]; 
    [webView stringByEvaluatingJavaScriptFromString:setImageRule]; 

    [webView stringByEvaluatingJavaScriptFromString:varMySheet]; 

    [webView stringByEvaluatingJavaScriptFromString:addCSSRule]; 

    [webView stringByEvaluatingJavaScriptFromString:insertRule1]; 

    [webView stringByEvaluatingJavaScriptFromString:insertRule2]; 

    [webView stringByEvaluatingJavaScriptFromString:setTextSizeRule]; 

    int totalWidth = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollWidth"] floatValue]; 
    pageCount = (ceilf)((float)totalWidth/webView.frame.size.width); 

    if (totalWidth % (int)webView.frame.size.width != 0) 
    { 
     UIScrollView *scrollV = [self getScrollViewFromWebView:webView]; 
     scrollV.contentSize = CGSizeMake(pageCount*(webView.frame.size.width), webView.scrollView.contentSize.height); 

    } 


    NSLog(@"Chapter %d: %@ -> %d pages & totalWidth :: %d", chapterIndex, title, pageCount,totalWidth); 

    [activityIndicator removeFromSuperview]; 
    [activityIndicator stopAnimating]; 
    [webView dealloc]; 


    [delegate chapterDidFinishLoad:self]; 

} 

에서

: -이 당신을 도울 것입니다

- (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-LEFT: 25px; padding-top: 100px; padding-right: 25px; height: %fpx; -webkit-column-gap: 50px; -webkit-column-width:%fpx;')", webView.frame.size.height-200, (webView.frame.size.width/2-100) ]; 

    NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding-left: 50px;padding-right: 50px; padding-top: 50px; padding-bottom: 50px; -webkit-column-gap: 100px;height: %fpx; -webkit-column-width : %fpx')", webView.frame.size.height-100,webView.frame.size.width/2-200];  


    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;')"]; 

    NSString *setImageRule = [NSString stringWithFormat:@"addCSSRule('img', 'max-width: %fpx; height:%fpx;')", (webView.frame.size.width/2)-200,webView.frame.size.height-200]; 

    NSString *setFontRule = [NSString stringWithFormat:@"addCSSRule('body', 'font-family: Arial;' , 'font-name: Arial-BoldItalicMT;')"]; 

    [webView stringByEvaluatingJavaScriptFromString:setFontRule]; 

    [webView stringByEvaluatingJavaScriptFromString:setImageRule]; 

    [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]; 
    } 


    float pageCountofChapter = [[loadedEpub.spineArray objectAtIndex:currentSpineIndex] pageCount]; 
    pagesInCurrentSpineCount =pageCountofChapter; 

    int totalWidth = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollWidth"] floatValue]; 

    if (totalWidth % (int)webView.frame.size.width != 0) 
    { 
     UIScrollView *scrollV = [self getScrollViewFromWebView:webView]; 
     scrollV.contentSize = CGSizeMake(pagesInCurrentSpineCount*(webView.frame.size.width), webView.scrollView.contentSize.height); 

    } 

    [self gotoPageInCurrentSpine:currentPageInSpineIndex]; 
} 

희망. 귀하의 요구 사항에 따라 패딩과 여백을 정렬하십시오.

+1

감사합니다.이 메소드에서 수행하는 작업은 무엇입니까? UIScrollView * scrollV = [ self getScrollViewFromWebView : webView]; – dineshprasanna

1

페이지 폭의 배수가 아닐 때 webview의 scrollview에 대한 열 번호와 관리 내용을 제공하여 논리를 변경했습니다. 시간 내 주셔서 감사합니다

+0

안녕하세요, 저는 또한 Aepubreader를 사용하고 있습니다. 가로 모드로 혼란 스럽습니다. 가로 모드를 어떻게 구현할 수 있습니까? – dineshprasanna

관련 문제