简体   繁体   English

如何在没有固定宽度的情况下将文本包装在 UILabel 中(通过 UIViewRepresentable)?

[英]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:这可确保:

  • the label will break line and not have an infinite width label 将断线并且没有无限宽度
  • the label will not add grow unnecessarily in height, which may happen in some circumstances. label 不会增加不必要的高度,这在某些情况下可能会发生。

Then...然后...

  • Add a width property to your UIViewRepresentable that will be used to set the preferredMaxLayoutWidth向您的UIViewRepresentable添加一个width属性,该属性将用于设置preferredMaxLayoutWidth
  • Use your UIViewRepresentable into a vanilla SwiftUI.View将您的UIViewRepresentable用于SwiftUI.View
  • Add a GeometryReader as an overlay to prevent expansion添加GeometryReader作为覆盖以防止扩展
  • Trigger the measurement after a soft delay, modifying some state to trigger a new pass.在软延迟后触发测量,修改一些 state 以触发新的通过。

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()
        }
    }

OLD ANSWER:旧答案:

... ...

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM