我正在ios上创建一个epub阅读器。我能够在uiwebview中显示epub,但是epub doest的大小不适合uiwebview。看来epub文件更大,而不是webview中的比例。我该怎么做才能调整它的大小?感谢。
这是我的代码。
vCover = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height)];
CGRect frame = busyLoad.frame;
frame.origin.x = self.view.frame.size.width / 2 - frame.size.width / 2;
frame.origin.y = self.view.frame.size.height / 2 - frame.size.height / 2;
busyLoad.frame = frame;
[self.view addSubview:vCover];
[self.view bringSubviewToFront:vCover];
[self.view addSubview:busyLoad];
[self.view bringSubviewToFront:busyLoad];
[busyLoad startAnimating];
int offset = [[NSString stringWithFormat:@"%@", todo.htmOffset]intValue]*webView.bounds.size.height;
NSLog(@"pageH:%f ", webView.bounds.size.height);
UIScrollView *scrollview = (UIScrollView *)[webView.subviews objectAtIndex:0];
scrollview.contentInset = UIEdgeInsetsMake(44.0,0.0,44.0,0.0);
NSString* javascript = [NSString stringWithFormat:@"window.scrollBy(0, %d);", offset];
[webView stringByEvaluatingJavaScriptFromString:javascript];
NSString *padding = @"document.body.style.margin='50';";
[webView stringByEvaluatingJavaScriptFromString:padding];
NSString *setTextSizeRule = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')", 100];
[webView stringByEvaluatingJavaScriptFromString:setTextSizeRule];
NSLog(@"DidLoad:%d", offset);
[webView setHidden:NO];
[busyLoad removeFromSuperview];
[vCover removeFromSuperview];
[busyLoad stopAnimating];
答案 0 :(得分:1)
你好!!,
您可以像UIScrollView的ios7分页属性一样进行分页 (即webview.scrollview.pagingEnabled = yes)使用以下方法。
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[self setPagination:self];
}
- (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;')"];
[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]);
int pageCount = contentSize.width/webView.frame.size.width;
//move to page using offset
// CGPoint point = CGPointMake(0, 0);
// point = CGPointMake(indexOfPage*webView.frame.size.width, 0);
// self.scrollView.contentOffset = point;
}