Reputation: 175
I am creating an epub reader on ios. I'm able to show the epub in the uiwebview but the size of the epub doest not fit on uiwebview. It appears the epub file is bigger and not proportion in webview. What should I do to resize it? Thanks.
This is my code.
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];
Upvotes: 1
Views: 493
Reputation: 1454
Hi there!!,
You can do pagination like ios7 pagination property of UIScrollView (i.e webview.scrollview.pagingEnabled = yes) using below methods.
- (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;
}
Upvotes: 1