Is there a way that I can limit the text to 2 / 3 lines using SwiftUI?

我只是一个虾纸丫 提交于 2020-03-05 06:35:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!