Need to display html formatted text inside the tableview in SwiftUI but Text("Hi") is not allowing us to use Attributed text inside it.
So trying following code to display multiline HTML formatted text inside List. But with no success.
And cell height should be dynamic according to the content size height.
struct ContentView: View {
@State var text = "Hello World This is line with more than two line of code to display as multiline textview inside the List cell."
@State var bool: Bool = false
var body: some View {
List(0 ..< 5) { item in
TextView(text: self.$text)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
}
}
}
struct TextView: UIViewRepresentable {
@Binding var text: String
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIView(context: Context) -> UITextView {
let myTextView = UITextView()
myTextView.delegate = context.coordinator
myTextView.font = UIFont(name: "HelveticaNeue", size: 15)
myTextView.isScrollEnabled = false
myTextView.isEditable = false
myTextView.isUserInteractionEnabled = true
myTextView.backgroundColor = .clear //UIColor(white: 0.0, alpha: 0.05)
myTextView.translatesAutoresizingMaskIntoConstraints = false
myTextView.isScrollEnabled = false
return myTextView
}
func updateUIView(_ uiView: UITextView, context: Context) {
uiView.text = text
}
class Coordinator : NSObject, UITextViewDelegate {
var parent: TextView
init(_ uiTextView: TextView) {
self.parent = uiTextView
}
// with delegate methods implemented normally
}
}
