-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHTMLViewController.swift
More file actions
82 lines (70 loc) · 2.43 KB
/
HTMLViewController.swift
File metadata and controls
82 lines (70 loc) · 2.43 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import UIKit
import Common
/**
* A view controller containing a scrollable HTML text view.
*/
class HTMLViewController: UIViewController {
private let contentHtml: String
private let titleText: String
private let doneButtonText: String
private var textView: UITextView!
required init?(coder: NSCoder) {
abort()
}
init(contentHtml: String, titleText: String, doneButtonText: String) {
self.contentHtml = contentHtml
self.titleText = titleText
self.doneButtonText = doneButtonText
super.init(nibName: nil, bundle: nil)
}
override func loadView() {
self.view = UIScrollView()
}
override func viewDidLoad() {
self.title = self.titleText
self.navigationItem.rightBarButtonItem = UIBarButtonItem(
title: self.doneButtonText,
style: .done,
target: self,
action: #selector(self.close)
)
self.view.backgroundColor = .systemBackground
self.textView = UITextView()
self.textView
.autoLayoutInView(self.view)
.fillY(self.view)
.fillX(self.view.safeAreaLayoutGuide)
.activate()
self.textView.textContainerInset = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
self.textView.isScrollEnabled = false
self.textView.isEditable = false
// Creating NSAttributedString from HTML is extremely slow,
// so load it off the main thread
DispatchQueue.global(qos: .userInitiated).async {
let style = """
<style>
body {
font-family: -apple-system, sans-serif;
font: -apple-system-body;
}
h1 { font: -apple-system-headline; }
h2 { font: -apple-system-subheadline; }
</style>
"""
let html = style + self.contentHtml
let attrString = try! NSMutableAttributedString(
data: Data(html.utf8),
options: [.documentType: NSAttributedString.DocumentType.html],
documentAttributes: nil
)
DispatchQueue.main.async {
self.textView.attributedText = attrString
self.textView.textColor = .label
}
}
}
@objc
private func close() {
self.dismiss(animated: true)
}
}