Следуя инструкциям, я пришел к этому решению UIViewRepresentable, используя NSAttributedString.
Код: Выделить всё
struct HTMLText: UIViewRepresentable {
var attributedText: NSAttributedString
init(text: String) {
self.attributedText = text.htmlToAttributedString
}
func makeUIView(context: Context) -> UITextView {
let uiTextView = UITextView()
uiTextView.backgroundColor = .red
uiTextView.textContainerInset = .zero
uiTextView.isEditable = false
uiTextView.isScrollEnabled = false
uiTextView.textContainer.lineFragmentPadding = 0
return uiTextView
}
func updateUIView(_ uiTextView: UITextView, context: Context) {
uiTextView.attributedText = attributedText
}
}
extension String {
var htmlToAttributedString: NSAttributedString {
guard let data = data(using: .utf8) else {
return NSAttributedString(string: self) }
do {
return try NSAttributedString(
data: data,
options: [.documentType: NSAttributedString.DocumentType.html,
.characterEncoding:String.Encoding.utf8.rawValue],
documentAttributes: nil)
} catch {
return NSAttributedString(string: self)
}
}
}
Эта репликация вызывает циклы, хотя их всего несколько, и отображается текст:
Код: Выделить всё
struct ContentView: View {
@State var showView: Bool = false
var body: some View {
NavigationStack {
Button(action: {
showView.toggle()
}, label: {
Text("Show view")
})
.navigationDestination(isPresented: $showView, destination: {
ScrollableView() // Putting the scroll view here directly seems to be fine
})
}
}
}
struct ScrollableView: View {
var body: some View {
ScrollView {
HTMLText(text: "hello world ")
}
}
}
Код: Выделить всё
struct Crash: View {
var body: some View {
GeometryReader { _ in
HTMLText(text: "hello world ")
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... or-crashes
Мобильная версия