forked from kean/Pulse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRichTextView-watchos.swift
51 lines (44 loc) · 1.19 KB
/
RichTextView-watchos.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
// The MIT License (MIT)
//
// Copyright (c) 2020-2024 Alexander Grebenyuk (github.com/kean).
import SwiftUI
import Pulse
#if os(watchOS) || os(macOS)
struct RichTextView: View {
let viewModel: RichTextViewModel
var body: some View {
ScrollView {
if let string = viewModel.attributedString {
Text(string)
} else {
Text(viewModel.text)
}
}
#if os(watchOS)
.toolbar {
if #available(watchOS 9, *) {
ShareLink(item: viewModel.text)
}
}
#endif
}
}
#endif
final class RichTextViewModel: ObservableObject {
let text: String
let attributedString: AttributedString?
var isLinkDetectionEnabled = true
var isEmpty: Bool { text.isEmpty }
init(string: String) {
self.text = string
self.attributedString = nil
}
init(string: NSAttributedString, contentType: NetworkLogger.ContentType? = nil) {
#if os(macOS)
self.attributedString = try? AttributedString(string, including: \.appKit)
#else
self.attributedString = try? AttributedString(string, including: \.uiKit)
#endif
self.text = string.string
}
}