我已经能够使NSAttributedStrings可通过UIViewRepresentable它,直到我换一个角度的伟大工程ScrollView。
当放置在 中时ScrollView,NSAttributedString视图停止渲染。
我已经尝试了一些其他的方法是替换NSAttributedString与添加多个Text()视图合力得到格式化其内部运作ScrollView和支持斜体和monospace font。不幸的是,这不适用于文本块内的链接,这意味着我仍然需要一个NSAttributedString.
import SwiftUI
struct TextWithAttributedString: UIViewRepresentable {
var attributedString: NSAttributedString
init(_ attributedString: NSAttributedString) {
self.attributedString = attributedString
}
func makeUIView(context: Context) -> UITextView {
let textView = UITextView(frame: .zero)
textView.attributedText = self.attributedString
textView.isEditable = false
return textView
}
func updateUIView(_ textView: UITextView, context: Context) {
textView.attributedText = self.attributedString
}
}
let exampleText …Run Code Online (Sandbox Code Playgroud)