0

I'm working with a wrapped UITextView via UIViewRepresentable. This textView is supposed to be as high es required, based on its content, i.e. the (attributed)string.

struct TextView: UIViewRepresentable {
    @Binding var text: String

    func makeUIView(context: Context) -> UITextView {
        let textView = UITextView()
        textView.isScrollEnabled = false
        textView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)

        return textView
    }

    func updateUIView(_ uiView: UITextView, context: Context) {
        uiView.text = text
    }
}

This works fine:

struct TextView_Previews: PreviewProvider {
    static var previews: some View {
        TextView(text: .constant("some multiline\na\nb\nc\nd\nlorem ipsum"))
    }
}

Doesn't work

struct TextView_Previews: PreviewProvider {
    static var previews: some View {
        ScrollView { // <- when placed inside ScrollView the height is reduced to one line
            TextView(text: .constant("some multiline\na\nb\nc\nd\nlorem ipsum"))
        }
    }
}

How can this be fixed?

benrudhart
  • 1,406
  • 2
  • 13
  • 25
  • Probably duplicate of [How to use an NSAttributedString with a ScrollView in SwiftUI?](https://stackoverflow.com/questions/59889020/how-to-use-an-nsattributedstring-with-a-scrollview-in-swiftui) – Asperi Jun 10 '20 at 11:42
  • Indeed that fixed it. Thank you! – benrudhart Jun 10 '20 at 13:03

1 Answers1

0
struct TextView_Previews: PreviewProvider {
    static var previews: some View {
        ScrollView { // <- when placed inside ScrollView the height is reduced to one line
            TextView(text: .constant("some multiline\na\nb\nc\nd\nlorem ipsum"))
                    .frame(minHeight: 100)

        }
    }
}
szz
  • 1