只有在将 html 渲染为 webview 后,您才能在书脊中找到页面数。我做了如下
- (void) setPagination:(UIWebView *) webView
{
NSString *varMySheet = @"var mySheet = document.styleSheets[0];";
NSString *addCSSRule = @"function addCSSRule(selector, newRule) {"
"ruleIndex = mySheet.cssRules.length;"
"mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);" // For Firefox, Chrome, etc.
"}";
NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', '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];
[webView stringByEvaluatingJavaScriptFromString:varMySheet];
[webView stringByEvaluatingJavaScriptFromString:addCSSRule];
[webView stringByEvaluatingJavaScriptFromString:insertRule1];
[webView stringByEvaluatingJavaScriptFromString:insertRule2];
CGSize contentSize = CGSizeMake([[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollWidth;"] floatValue],
[[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] floatValue]);
self.pageVO.chapterVO.pageCountInChapter = contentSize.width/webView.frame.size.width;
// NSLog(@"content width : %f ,frame width : %f ,my page count : %f",contentSize.width,webView.frame.size.width,contentSize.width/webView.frame.size.width);
CGPoint point = CGPointMake(0, 0);
if([self checkIsPageIndexOutOfRange])
{
//after decreasing font size page will be left blank
[self.pageVO setIndexOfPage:self.pageVO.chapterVO.pageCountInChapter-1];
[_myDelegate myWebViewOnPageOutOfRange];
}
else
{
if([self.pageVO getIndexOfPage] ==PAGE_INDEX_GREATER_THAN_PAGE_COUNT)
{
//load last page of chapter
[self.pageVO setIndexOfPage:self.pageVO.chapterVO.pageCountInChapter-1];
}
if([self.pageVO getIndexOfPage] !=GET_PAGE_INDEX_USING_WORD_ID)
{
point = CGPointMake([self.pageVO getIndexOfPage]*webView.frame.size.width, 0);
self.scrollView.contentOffset = point;
}
}
[_myDelegate myWebViewDidLoadFinish];
}