Repository navigation
Expand file tree
/
Copy pathReflectViewer.swift
More file actions
68 lines (55 loc) · 1.93 KB
/
Copy pathReflectViewer.swift
File metadata and controls
68 lines (55 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// ReflectViewer.swift
// HighLow
//
// Created by Caleb Hester on 6/27/20.
// Copyright © 2020 Caleb Hester. All rights reserved.
//
import UIKit
import WebKit
import Gridicons
class ReflectViewer: WKWebView, UITextFieldDelegate {
var contentScrollHeight: CGFloat = 0.0
var hasLoaded: Bool = false
override init(frame: CGRect, configuration: WKWebViewConfiguration) {
super.init(frame: frame, configuration: configuration)
setup()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setup()
}
private func setup() {
self.scrollView.contentInsetAdjustmentBehavior = .never
}
func load() {
if !hasLoaded {
let url = Bundle.main.url(forResource: "view", withExtension: "html")!
self.loadFileURL(url, allowingReadAccessTo: url)
hasLoaded = true
}
}
func loadBlocks(_ blocks: [NSDictionary], completion: @escaping () -> Void = {}) {
do {
let blocksString = try JSONSerialization.data(withJSONObject: blocks, options: .prettyPrinted)
let jsonStr = NSString(data: blocksString, encoding: String.Encoding.utf8.rawValue)! as String
self.evaluateJavaScript("setBlocks(\(jsonStr))", completionHandler: { blah, blah2 in
self.evaluateJavaScript("document.body.scrollHeight") { result, error in
guard error == nil else {
completion()
return
}
self.contentScrollHeight = result as! CGFloat
completion()
}
})
} catch {
//self.load()
}
}
override var intrinsicContentSize: CGSize {
get {
return CGSize(width: self.scrollView.contentSize.width, height: self.contentScrollHeight)
}
}
}