SwiftUI:我的视图中的文本被“…”截断

6
在我的观点中,我有很多文本并且周围有边框,但有时会被截断并在结尾处显示“...”。我已经尝试使用 .layoutPriority().fixedSize(),但奇怪的是,根据我使用 .layoutPriority() 的位置和频率不同,它在不同的设备上被截断的位置也不同。iPhone 8 iOS 13.3 模拟器中的屏幕截图
谢谢您的帮助。以下是我的当前代码:
import SwiftUI
import SafariServices

struct WhatIsStopView: View {

    @State var show4 = false

    var body: some View {
        GeometryReader { geometry in
            ScrollView(.vertical) {
            VStack(alignment: .leading, spacing: 30) {
                HStack(spacing: 15) {
                    Text("whatIsAStop")
                    Image(systemName: "camera")
                }.font(.largeTitle)
                VStack(alignment: .leading, spacing: 45) {
                    VStack(alignment: .leading, spacing: 25) {
                        Text("inPhotographyAStop").layoutPriority(2)

                        Text("stopDoubling").fontWeight(.semibold).layoutPriority(2)

                        Text("forExample").layoutPriority(2)

                        VStack {
                            Image("exposure-stops").resizable()
                                        .aspectRatio(contentMode: .fit)
                            Text("stopIsAMeasure").italic().layoutPriority(2)
                        }

                        Text("amountOfLightCaptured").layoutPriority(2)

                    }

                    VStack(alignment: .leading, spacing: 25) {
                        Text("stopsAndShutterSpeed").font(.headline).layoutPriority(2)

                        Text("shutterSpeedMeasures").layoutPriority(2)

                        VStack {
                            Image("exposure-stops-shutter-speed").resizable()
                                .aspectRatio(contentMode: .fit)
                            Text("commonShutterSpeedStops").italic().layoutPriority(2)
                        }

                        Text("forExampleChanging").layoutPriority(2)

                        Text("mostCamerasAllow").layoutPriority(2)
                    }

                    VStack(alignment: .leading, spacing: 25) {
                        Text("stopsAndISOSpeed").font(.headline).layoutPriority(2)

                        Text("ISOSpeedDescribes").layoutPriority(2)

                        VStack {
                            Image("exposure-stops-iso-speed").resizable()
                            .aspectRatio(contentMode: .fit)
                            Text("commonISOSpeedStops").italic().layoutPriority(2)
                        }

                        Text("ISOIsMeasured").layoutPriority(2)

                        Text("forExampleSwitching").layoutPriority(2)
                    }

                    VStack(alignment: .leading, spacing: 25) {
                        Text("stopsAndAperture").font(.headline).layoutPriority(2)

                        Text("apertureIsMeasured").layoutPriority(2)

                        VStack {
                            Image("exposure-stops-aperture").resizable()
                            .aspectRatio(contentMode: .fit)
                            Text("commonApertureStops").italic().layoutPriority(2)
                        }

                        Text("becauseOfTheWay").layoutPriority(2)

                        Text("asWithShutterSpeed").layoutPriority(2)
                    }

                    VStack(alignment: .leading, spacing: 25) {

                            Text("stopsAreInterchangeable").font(.headline).layoutPriority(2)

                            Text("theGreatThing").layoutPriority(2)

                            Text("letsSay").layoutPriority(2)

                            Text("thisChangeOf").layoutPriority(2)

                            Text("youCouldOpen").layoutPriority(2)

                            Text("alternativelyYouCould").layoutPriority(2)

                            Text("asYouCanSee").layoutPriority(2)
                    }

                    VStack(alignment: .leading, spacing: 25) {

                            Text("conderationsWhenAdjustingExposure").font(.headline).layoutPriority(2)

                            Text("whenAdjustingTheThree").layoutPriority(2)

                            Text("shutterSpeedIf").layoutPriority(2)

                            Text("apertureA").layoutPriority(2)

                            Text("ISOSpeedThe").layoutPriority(2)

                            Text("asWithEverything").layoutPriority(2)

                            Text("exposureStopsAreA").layoutPriority(2)
                    }

                    Divider()

                    HStack {
                        Button(action: {
                            self.show4.toggle()
                        }) {
                            Text("linkToOriginalArticle").padding().foregroundColor(.white)
                                .background(LinearGradient(gradient: .init(colors: [.yellow,.purple]), startPoint: .leading, endPoint: .trailing))
                            .cornerRadius(20)
                                .sheet(isPresented: self.$show4) {
                                    photography().edgesIgnoringSafeArea(.all)
                            }
                        }
                        Spacer()
                        Text("© Photography Mad")
                    }

                    }.padding()
                .overlay(
                    RoundedRectangle(cornerRadius: 16)
                        .stroke((LinearGradient(gradient: .init(colors: [.purple,.blue]), startPoint: .leading, endPoint: .trailing)), lineWidth: 4)
                )
                Spacer()
            }.padding()
                VStack {
                    AdView().frame(width: 320, height: 50)
                }.edgesIgnoringSafeArea([.top, .leading, .trailing])
        }
    }
        .navigationBarTitle(Text("whatIsAStop"), displayMode: .inline)
    }
}

struct WhatIsStopView_Previews: PreviewProvider {
    static var previews: some View {
        WhatIsStopView()
            .environment(\.locale, .init(identifier: "fr"))
    }
}


struct photography : UIViewControllerRepresentable {

    func makeUIViewController(context: UIViewControllerRepresentableContext<photography>) -> SFSafariViewController {
        let controller = SFSafariViewController(url: URL(string: "https://www.photographymad.com/pages/view/what-is-a-stop-of-exposure-in-photography")!)
        return controller
    }
    func updateUIViewController(_ uiViewController: SFSafariViewController, context: UIViewControllerRepresentableContext<photography>) {

    }
}

你使用了 .lineLimit(nil) 吗? - E.Coms
我尝试将它放在VStack的Text和其他元素旁边,但仍然出现了“错误”。 - Lukas Apple Fan
你能否直接给我们展示一个更小的例子以直接解决这个 bug 吗? - E.Coms
@E.Coms 这是一个带有圆圈标记的错误截图:截图链接 - Lukas Apple Fan
2个回答

4
在iOS 14中,你可以尝试:
对于水平扩展,请尝试:
.fixedSize(horizontal: false, vertical: true)

如果要进行垂直扩展,请尝试

.fixedSize(horizontal: true, vertical: false)

经过尝试了很多方法,最终你的方法非常有效,非常感谢。 - Kishore Kumar
经过尝试了很多方法,最终你的方法很有效,非常感谢。 - undefined

1
行为非常正常。
布局优先级允许您控制视图在空间有限时收缩的程度或在空间充足时扩展的程度。所有视图默认具有0的布局优先级,这意味着它们每个都有平等的机会增长或缩小。当您将文本的布局优先级设置为1(高于0)时,它将自动占用所有可用空间。
当您将所有 Textfields 设置为 layoutPriority(1)时,这意味着所有文本字段再次具有相同的优先级,并且视图决定哪个将扩展。
您可以通过以下示例进行检查。
struct layout: View {

let text: String = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."

    var body: some View {
        VStack(alignment: .leading, spacing: 25) {
            Text(text)
            Text(text)
            Text(text)
        }
    }
}

现在将 VStack 嵌入到 scrollView 中。由于 scrollView 自动扩展,当视图无法适应时,所有的 Textfields 将显示完整的文本,而不需要使用布局优先级。
你看,弹性视图通常不需要布局优先级。
在你的代码中,你使用了一个 geometryReader,但没有设置约束条件。所以请检查你的代码,如果 geometryReader 限制了视图的大小。

12
非常感谢,但我只是在每个 Text() 旁边使用了.fixedSize(horizontal: false, vertical: true),它就可以正常工作了。 - Lukas Apple Fan
1
谢谢@LukasAppleFan,这确实帮了我很多,也许你可以发布一个回答来解决自己的问题? - smat88dd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接