Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extension WKWebView {
- func contentSize(completion : @escaping (_ size: CGSize?, _ error: Error?) -> ()){
- self.evaluateJavaScript("document.body.scrollHeight;") { (scrollHeight, error) in
- if let height = scrollHeight as? CGFloat {
- self.evaluateJavaScript("document.body.scrollWidth;") { (scrollWidth, error) in
- if let width = scrollWidth as? CGFloat {
- let sizeThatFits = self.sizeThatFits(.zero)
- let widthPercentage = (width / sizeThatFits.width) * 100
- let requiredHeight = (height / widthPercentage) * 100
- let size = CGSize.init(width: sizeThatFits.width, height: requiredHeight)
- completion(size, nil)
- } else {
- completion(nil, error)
- }
- }
- } else {
- completion(nil, error)
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment