UIViewRepresentable of UITextView in SwiftUI layout issues

I'm trying to show a list with UITextView in the cells to display text the user can select. The text gets clipped, and if I specify "isScrollEnabled = false" then the text no longer wraps.


Here's a simple version of my code:


struct UITextViewContainer: UIViewRepresentable {
    let text: String
    
    func makeUIView(context: UIViewRepresentableContext<Self>) -> UITextView {
        let view = UITextView()
        view.isScrollEnabled = false
        view.isEditable = false
        return view
    }
    
    func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext<Self>) {
        uiView.text = self.text
    }
}

struct ContentView : View {
    var body: some View {
        List(1...5) { item in
            UITextViewContainer(text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur quis pharetra diam, rhoncus tristique orci. Nam semper interdum facilisis. Sed mollis ligula ac odio iaculis iaculis. Ut consectetur bibendum augue, at rutrum metus semper ut. Nam molestie, arcu sed commodo facilisis, erat sem consectetur sem, a mattis nisi libero cursus sapien.")
        }
    }
}

With isScrollEnabled = true

With isScrollEnabled = false

Have you had any luck with this? I've played around a bit with changing hugging and restance priorities and get it closer to what it needs to be, but the vertical size is still wrong...


import SwiftUI

struct UITextViewContainer: UIViewRepresentable {
    let text: String
    
    func makeUIView(context: UIViewRepresentableContext<Self>) -> UITextView {
        let view = UITextView()
        view.isScrollEnabled = false
        view.isEditable = false
        view.dataDetectorTypes = .all
        view.isSelectable = true
        return view
    }
    
    func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext<Self>) {
        uiView.text = self.text
        uiView.setContentHuggingPriority(.defaultHigh, for: .vertical)
        uiView.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        uiView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        uiView.setContentCompressionResistancePriority(.required, for: .vertical)
        
        

        
        
    }
}

#if DEBUG
struct UITextViewContainer_Previews: PreviewProvider {
    static var previews: some View {
        VStack {
            UITextViewContainer(text: "Lorum ipsum dsffds sdf fsd fsd fsd fds fsd sdf sdf sdf fsd dfs fsd fds fsdsfd dsf fds sfdfds fsd  fsd fsd dsffsdflkdsf fsd fsd sdfsdf super long text woot woot und hier.")
            UITextViewContainer(text: "Lorum ipsum dsffds sdf fsd fsd fsd fds fsd sdf fsd fsd dsffsdflkdsf fsd fsd sdfsdf super long text woot woot.")
            UITextViewContainer(text: "Lorum ipsum dsffds sdf fsd fsd fsd fds fsd sdf fsd fsd dsffsdflkdsf fsd fsd sdfsdf super long text woot woot.")

        }
  }
}
#endif
You should disable scroll after UITextView finished layout
Code Block
func makeUIView(context: UIViewRepresentableContext<Self>) -> UITextView {
let view = UITextView()
view.isScrollEnabled = false
......
return view
}
    func updateUIView(_ uiView: UITextView, context: Context) {
        if uiView.frame.size != .zero {
            uiView.isScrollEnabled = false
        }
    }

  • ayer 6 de abril(2023) a las 10pm, se bajaron al UIViewRepresentable y tb al NSViewRepresentable
  • ya no funcionara hasta cuando ...

  • I think they do it on purpose
UIViewRepresentable of UITextView in SwiftUI layout issues
 
 
Q