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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With