Skip to content

Instantly share code, notes, and snippets.

@c1800054work
Created March 22, 2022 09:11
Show Gist options
  • Select an option

  • Save c1800054work/b5410fa5692dade5a93a986810243f01 to your computer and use it in GitHub Desktop.

Select an option

Save c1800054work/b5410fa5692dade5a93a986810243f01 to your computer and use it in GitHub Desktop.
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