Skip to content

Instantly share code, notes, and snippets.

@viniciussoares
Last active June 28, 2019 13:57
Show Gist options
  • Save viniciussoares/a7e97fa57c9eb266d049d5189b2f6c58 to your computer and use it in GitHub Desktop.
Save viniciussoares/a7e97fa57c9eb266d049d5189b2f6c58 to your computer and use it in GitHub Desktop.
A WebView that automatically resizes itself - iOS - Swift
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<style>
@font-face {
font-family: 'Uni Neue';
src: local('Uni Neue'), url('UniNeueRegular.otf') format('opentype');
font-weight: 400;
}
@font-face {
font-family: 'Uni Neue';
src: local('Uni Neue'), url('UniNeueRegular-Italic.otf') format('opentype');
font-style: italic;
font-weight: 400;
}
@font-face {
font-family: 'Uni Neue';
src: local('Uni Neue'), url('UniNeueBold.otf') format('opentype');
font-weight: bold;
}
body {
font-family: 'Uni Neue', -apple-system;
}
ul {
list-style: none;
}
ul li::before {
content: "●";
color: #14AA4B;
font-weight: bold;
display: inline-block;
width: 1em;
margin-left: -1em;
}
</style>
</head>
<body>
HTML_CONTENT
</body>
</html>
import UIKit
import WebKit
final public class HtmlContentView: UIView, WKNavigationDelegate {
private let webView: WKWebView = {
let webConfiguration = WKWebViewConfiguration()
let view = WKWebView(frame: .zero, configuration: webConfiguration)
view.backgroundColor = .clear
view.isOpaque = false
view.scrollView.bounces = false
view.scrollView.isScrollEnabled = false
view.scrollView.showsVerticalScrollIndicator = false
return view
}()
override public var intrinsicContentSize: CGSize {
return CGSize(width: super.intrinsicContentSize.width, height: contentHeight)
}
private var contentHeight: CGFloat = 20 {
didSet { invalidateIntrinsicContentSize() }
}
override public init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
private func setup() {
backgroundColor = .clear
addSubview(webView)
webView.frame = bounds
webView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
webView.navigationDelegate = self
}
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation) {
webView.evaluateJavaScript("document.body.scrollHeight;") { [weak self] (result, _) in
let height = result as? NSNumber ?? 400
self?.contentHeight = CGFloat(height.floatValue)
}
}
public func show(html: String) {
contentHeight = 20.0
do {
let bundle = Bundle(for: HtmlContentView.self)
guard let path = bundle.path(forResource: "HtmlContentView", ofType: "html") else { return }
let template = try String(contentsOfFile: path, encoding: String.Encoding.utf8)
let html = template.replacingOccurrences(of: "HTML_CONTENT", with: html)
webView.loadHTMLString(html, baseURL: NSURL.fileURL(withPath: bundle.bundlePath))
} catch {
print("\(#file) - \(error)")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment