Created
March 22, 2022 09:11
-
-
Save c1800054work/b5410fa5692dade5a93a986810243f01 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private func setWebView(_ data: String){ | |
| let jscript = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);" | |
| let userScript = WKUserScript(source: jscript, injectionTime: .atDocumentEnd, forMainFrameOnly: true) | |
| let wkUController = WKUserContentController() | |
| wkUController.addUserScript(userScript) | |
| let wkWebConfig = WKWebViewConfiguration() | |
| wkWebConfig.userContentController = wkUController | |
| let infoWebview = WKWebView(frame: self.contentView.bounds, configuration: wkWebConfig) | |
| contentView.addSubview(infoWebview) | |
| infoWebview.translatesAutoresizingMaskIntoConstraints = false | |
| infoWebview.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true | |
| infoWebview.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true | |
| infoWebview.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true | |
| infoWebview.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true | |
| let cssTemplate = """ | |
| <html> | |
| <head> | |
| <style> | |
| figure { | |
| margin: 0; | |
| padding: 0; | |
| max-width: 100% !important; | |
| } | |
| img { | |
| max-width: 100% !important; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| \(data) | |
| </body> | |
| </html> | |
| """ | |
| infoWebview.loadHTMLString(cssTemplate, baseURL: nil) | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment