Setting lineBreakMode to byWordWrapping and set numberOfLines to 0 does not seem to be sufficient:
struct MyTextView: UIViewRepresentable {
func makeUIVi
I found a somehow "nasty" approach that allows a UILabel
to properly wrap when used as a UIViewRepresentable
(even when inside a ScrollView
), without the need for GeometryReader
:
Whenever creating your UILabel
:
label.setContentCompressionResistancePriority(.defaultLow,
for: .horizontal)
label.setContentHuggingPriority(.defaultHigh,
for: .vertical)
This ensures that:
Then...
width
property to your UIViewRepresentable
that will be used to set the preferredMaxLayoutWidth
UIViewRepresentable
into a vanilla SwiftUI.View
GeometryReader
as an overlay to prevent expansioni.e.:
public var body: some View {
MyRepresentable(width: $width,
separator: separator,
content: fragments)
.overlay(geometryOverlay)
.onAppear { self.shouldReadGeometry = true }
}
// MARK: - Private Props
@State private var width: CGFloat?
@State private var shouldReadGeometry = false
var geometryOverlay: some View {
if shouldReadGeometry {
return GeometryReader { g in
SwiftUI.Color.clear.onAppear {
self.width = g.size.width
}
}.eraseToAnyView()
} else {
return EmptyView().eraseToAnyView()
}
}
...
In your updateUIView(_:context:)
:
if let sv = uiView.superview, sv.bounds.width != 0 {
let shouldReloadState = uiView.preferredMaxLayoutWidth != sv.bounds.width
uiView.preferredMaxLayoutWidth = sv.bounds.width
if shouldReloadState {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
self.stateToggle.toggle() // a Bool @State you can add in your struct
}
}
}
Disclaimer: I'm not a huge fan of
main.async
calls, particularly when they come in combination with some arbitrary delay, but this seems to get the work done in a consistent way.