-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathContentView.swift
75 lines (65 loc) · 2.16 KB
/
ContentView.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
// Created by Marcin Krzyzanowski
// https://github.com/krzyzanowskim/STTextView/blob/main/LICENSE.md
import SwiftUI
import STTextViewSwiftUI
#if canImport(AppKit) && !targetEnvironment(macCatalyst)
typealias Font = NSFont
typealias Color = NSColor
let textColor = Color.textColor
#endif
#if canImport(UIKit)
typealias Font = UIFont
typealias Color = UIColor
let textColor = Color.label
#endif
struct ContentView: View {
@State private var text: AttributedString = ""
@State private var selection: NSRange?
@State private var counter = 0
@State private var font = Font.monospacedSystemFont(ofSize: 0, weight: .medium)
var body: some View {
VStack(spacing: 0) {
// this is fast
TextView(
text: $text,
selection: $selection,
options: [.wrapLines, .highlightSelectedLine, .showLineNumbers]
)
.textViewFont(font)
// Button("Modify") {
// text.insert(AttributedString("\(counter)\n"), at: text.startIndex)
// counter += 1
// selection = NSRange(location: 0, length: 3)
// }
// SwiftUI is slow, I wouldn't use it
//
// SwiftUI.TextEditor(text: Binding(get: { String(text.characters) }, set: { text = AttributedString($0) }))
// .font(.body)
HStack {
if let selection {
Text("Location: \(selection.location)")
} else {
Text("No selection")
}
Spacer()
}
.padding(.vertical, 4)
.padding(.horizontal, 8)
}
.onAppear {
loadContent()
}
}
private func loadContent() {
let string = try! String(contentsOf: Bundle.main.url(forResource: "content", withExtension: "txt")!)
self.text = AttributedString(
string.prefix(4096),
attributes: AttributeContainer([.foregroundColor: textColor, .font: font])
)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}