Есть ли способ, которым я могу ограничить текст 2/3 строк, используя SwiftUI? - PullRequest
0 голосов
/ 06 июня 2019

Я пытаюсь использовать

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 строк.

1 Ответ

1 голос
/ 06 июня 2019

Вызовите .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?>>
...