有没有一种方法可以使用SwiftUI将文本限制为2/3行? [英] Is there a way that I can limit the text to 2 / 3 lines using SwiftUI?

查看:44
本文介绍了有没有一种方法可以使用SwiftUI将文本限制为2/3行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用

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行.

It will wrap the text to say n number of lines.

推荐答案

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

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.)

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?>>

这篇关于有没有一种方法可以使用SwiftUI将文本限制为2/3行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆