2

我正在尝试使用

var body: some View {
    Text("This is large text. Is there a way that I can unwrap the large text as discussed").lineLimit(2)
}

仅供参考: 我知道

var body: some View {
    Text("This is large text. Is there a way that I can unwrap the large text as discussed").lineLimit(nil)
}

它会将文本包装成 n 行。

4

2 回答 2

2

调用元素.lineLimit(3)Text(从技术上讲,它可以在any View上调用,在这种情况下,它将限制该视图中所有 Text元素的行。)

来自SwiftUI.View

    /// Sets the maximum number of lines that text can occupy in this view.
    ///
    /// The line limit applies to all `Text` instances within this view. For
    /// example, an `HStack` with multiple pieces of text longer than three
    /// lines caps each piece of text to three lines rather than capping the
    /// total number of lines across the `HStack`.
    ///
    /// - Parameter number: The line limit. If `nil`, no line limit applies.
    /// - Returns: A view that limits the number of lines that `Text` instances
    ///   display.
    public func lineLimit(_ number: Int?) -> Self.Modified<_EnvironmentKeyWritingModifier<Int?>>
于 2019-06-06T13:25:47.297 回答
0

因为暂时没有按预期工作,如果您有多个文本(如标题和子标题),则可以.linelimit简单地将 Text 元素包装在 VStack中元素 :.center.multilineTextAlignment(.center)

 VStack{
        Text("This is large text. Is there a way that I can unwrap the large text as discussed")
        .font(.title3)
        .foregroundColor(.white)
                             
        }.padding()
         .multilineTextAlignment(.center)    

                    
于 2021-03-04T17:21:33.107 回答