17

在android中,我们可以像这样设置文本视图的最大行数:

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"  
    android:maxLines="5" />

然而在颤振中,我们可以通过添加最大高度约束来做到这一点,但我们需要知道高度到底是多少。

有什么方法可以轻松指定 Text/RichText 的最大行数?

4

4 回答 4

37

使用maxLines属性简单

Text("Some large text", maxLines: 2)

您可以使用 在文本overflow: TextOverflow.ellipsis中插入...溢出省略号

Text("Some large text", maxLines: 2, overflow: TextOverflow.ellipsis)
于 2019-02-20T11:24:45.437 回答
10

编辑 2021 - 现在有可能

(提示 - 可选但推荐使用 maxLines 设置一些溢出)

        Text(
          'My text',
          maxLines: 4,
          overflow: TextOverflow.ellipsis,
        ),


       RichText(
          text: TextSpan(children: ....),
          maxLines: 4,
          overflow: TextOverflow.ellipsis,
        ),
于 2016-12-15T20:32:23.320 回答
8

我相信它现在得到支持:

RichText(
    maxLines: 2,
    overflow: TextOverflow.ellipsis, // TextOverflow.clip // TextOverflow.fade
    text: TextSpan(
        text: 'Hello ',
        style: DefaultTextStyle.of(context).style,
        children: <TextSpan>[
            TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)),
            TextSpan(text: ' world!'),
        ],
    ),
)
于 2018-11-04T15:50:59.900 回答
-3

使用简单的代码片段,您可以实现:

Text("Text",
    style: TextStyle(
        fontFamily: "Avenir",
        color: Color(0xff0C64B1),
        fontSize: width * 0.045),
    maxLines: 10)

如果您遇到上述任何问题,请告诉我。我很乐意为您提供帮助。

于 2021-02-24T11:32:56.137 回答