Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView page count

I have a WebView that displays some content, which is frequently updated. I would like to display a page counter in my UI which would tell the user how many pages would web view have if they would to print it.

I have tried doing:

NSRange r = NSMakeRange(0, 0);
BOOL knows = [[[[webView mainFrame] frameView] documentView] knowsPageRange: &r];

which gives knows = YES but r.length always equals 1.

How can I get at this information? (Preferably in a performant way, if possible).

like image 758
Jakub Hampl Avatar asked Apr 12 '13 11:04

Jakub Hampl


1 Answers

Not a perfectly elegant solution, but a possibility could include:

NSString *heightStr = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"]; // Get the height of our webView
int height = [heightStr intValue];

CGFloat maxHeight   = kDefaultPageHeight - 2*kMargin;
CGFloat maxWidth    = kDefaultPageWidth - 2*kMargin;
int pages = ceil(height / maxHeight);
return pages;

via Chris Mills' blog.

like image 183
The Kraken Avatar answered Nov 13 '22 18:11

The Kraken