generated from krzyzanowskim/STTextView-Plugin-Template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathViewController.swift
80 lines (59 loc) · 2.58 KB
/
ViewController.swift
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
import SwiftUI
import UIKit
import STTextView
import STAnnotationsPlugin
import STAnnotationsPluginShared
class ViewController: UIViewController, STAnnotationsDataSource {
@ViewLoading
private var textView: STTextView
@ViewLoading
private var annotationsPlugin: STAnnotationsPlugin
var textViewAnnotations: [any STLineAnnotation] = [] {
didSet {
annotationsPlugin.reloadAnnotations()
}
}
override func viewDidLoad() {
super.viewDidLoad()
let textView = STTextView()
textView.translatesAutoresizingMaskIntoConstraints = false
let defaultParagraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
defaultParagraphStyle.lineHeightMultiple = 1.2
textView.defaultParagraphStyle = defaultParagraphStyle
textView.font = UIFont.monospacedSystemFont(ofSize: 14, weight: .regular)
textView.highlightSelectedLine = true
textView.showsLineNumbers = true
textView.text = """
import Foundation
func main() {
print("Hello World!)
}
"""
view.addSubview(textView)
self.textView = textView
NSLayoutConstraint.activate([
textView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
textView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
textView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
])
annotationsPlugin = STAnnotationsPlugin(dataSource: self)
textView.addPlugin(
annotationsPlugin
)
// add annotation
let annotation1 = try! STMessageLineAnnotation(
id: UUID().uuidString,
message: AttributedString(markdown: "Swift Foundation framework"),
kind: .info,
location: textView.textLayoutManager.location(textView.textLayoutManager.documentRange.location, offsetBy: 17)!
)
let annotation2 = try! STMessageLineAnnotation(
id: UUID().uuidString,
message: AttributedString(markdown: "**ERROR**: Missing \" at the end of the string literal"),
kind: .error,
location: textView.textLayoutManager.location(textView.textLayoutManager.documentRange.location, offsetBy: 56)!
)
textViewAnnotations += [annotation1, annotation2]
}
}