[英]How can I get text to wrap in a UILabel (via UIViewRepresentable) without having a fixed width?
Setting lineBreakMode to byWordWrapping and set numberOfLines to 0 does not seem to be sufficient:将 lineBreakMode 设置为 byWordWrapping 并将 numberOfLines 设置为 0 似乎还不够:
struct MyTextView: UIViewRepresentable {
func makeUIView(context: Context) -> UILabel {
let label = UILabel()
label.lineBreakMode = .byWordWrapping
label.numberOfLines = 0
label.text = "Here's a lot of text for you to display. It won't fit on the screen."
return label
}
func updateUIView(_ view: UILabel, context: Context) {
}
}
struct MyTextView_Previews: PreviewProvider {
static var previews: some View {
MyTextView()
.previewLayout(.fixed(width: 300, height: 200))
}
}
The text does not wrap, regardless of which setting I use for lineBreakMode.无论我对 lineBreakMode 使用哪种设置,文本都不会换行。 The canvas preview and live preview both look like this:
canvas 预览和实时预览都如下所示:
The closest I've gotten is setting preferredMaxLayoutWidth, which does cause the text to wrap, but there doesn't seem to be a value that means "whatever size the View is".我得到的最接近的是设置preferredMaxLayoutWidth,这确实会导致文本换行,但似乎没有一个值表示“无论视图大小如何”。
Possible solution is to declare the width as a variable on MyTextView:可能的解决方案是将宽度声明为 MyTextView 上的变量:
struct MyTextView: UIViewRepresentable {
var width: CGFloat
func makeUIView(context: Context) -> UILabel {
let label = UILabel()
label.lineBreakMode = .byWordWrapping
label.numberOfLines = 0
label.preferredMaxLayoutWidth = width
label.text = "Here's a lot of text for you to display. It won't fit on the screen."
return label
}
func updateUIView(_ view: UILabel, context: Context) {
}
}
and then use GeometryReader to findout how much space there is avaible and pass it into the intializer:然后使用 GeometryReader 找出有多少可用空间并将其传递给初始化器:
struct ExampleView: View {
var body: some View {
GeometryReader { geometry in
MyTextView(width: geometry.size.width)
}
}
}
Try to use this magic line in makeUIView() func尝试在 makeUIView() 函数中使用这条神奇的线
label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
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
:我发现了一种“讨厌”的方法,它允许
UILabel
在用作UIViewRepresentable
时正确包装(即使在ScrollView
内),而不需要GeometryReader
:
Whenever creating your UILabel
:每当创建你的
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
添加一个width
属性,该属性将用于设置preferredMaxLayoutWidth
UIViewRepresentable
into a vanilla SwiftUI.View
UIViewRepresentable
用于SwiftUI.View
GeometryReader
as an overlay to prevent expansionGeometryReader
作为覆盖以防止扩展 ie: IE:
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:)
:在您的
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.免责声明:我不是
main.async
调用的忠实拥护者,尤其是当它们与一些任意延迟相结合时,但这似乎可以以一致的方式完成工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.