问题
I am trying using
var body: some View {
Text("This is large text. Is there a way that I can unwrap the large text as discussed").lineLimit(2)
}
FYI: I knew
var body: some View {
Text("This is large text. Is there a way that I can unwrap the large text as discussed").lineLimit(nil)
}
It will wrap the text to say n number of lines.
回答1:
Call .lineLimit(3)
on the Text
element. (Technically, it can be called on any View
, in which case it will limit the lines of all Text
elements in that view.)
From 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?>>
来源:https://stackoverflow.com/questions/56475779/is-there-a-way-that-i-can-limit-the-text-to-2-3-lines-using-swiftui