I’ve a UITextView
, inside a UIViewRepresentable
, so I can use it in a SwiftUI ScrollView
. Im utilizing intrinsic content material sizing to set the body of the content material. This works nice, till I set .isScrollEnabled
on the UITextView
to false – at that time the intrinsic content material measurement now not works. Im making an attempt to keep away from the opposite structure various the place I’ve to calculate and set the body measurement – this answer with intrinsic is way nicer. However I cant get across the .isScrollEnabled
breaking it. Heres the code:
struct MainView: View {
var physique: some View {
ScrollView {
SimpleTextView(textual content: loremIpsum)
.body(minHeight: 10)
.border(.crimson)
SimpleTextView(textual content: loremIpsum)
.body(minHeight: 10)
.border(.crimson)
SimpleTextView(textual content: loremIpsum)
.body(minHeight: 10)
.border(.crimson)
}
}
}
The UITextView:
struct SimpleTextView: UIViewRepresentable {
@State
non-public var attributedText: NSAttributedString?
var textual content: String
func makeUIView(context: Context) -> UITextView {
let view = ContentTextView()
view.setContentHuggingPriority(.required, for: .vertical)
view.setContentHuggingPriority(.required, for: .horizontal)
view.isSelectable = true
view.isEditable = false
view.isScrollEnabled = true
view.isUserInteractionEnabled = true
return view
}
func updateUIView(_ uiView: UITextView, context: Context) {
guard let attributedText = attributedText else {
generateAttributedText()
return
}
uiView.attributedText = attributedText
uiView.invalidateIntrinsicContentSize()
}
non-public func generateAttributedText() {
guard attributedText == nil else { return }
DispatchQueue.important.async {
self.attributedText = NSAttributedString(string: textual content, attributes: [:])
}
}
non-public class ContentTextView: UITextView {
override var canBecomeFirstResponder: Bool { false }
override var intrinsicContentSize: CGSize {
body.top > 0 ? contentSize : tremendous.intrinsicContentSize
}
}
}
Output when isScrollEnabled is true:
When isScrollEnabled
is fake, i simply get an empty web page.
Thanks for any help.