// This is the text we are testing with
String text = 'Text Example';
// This is the width of the SizedBox/Container
double width = 30;
// maxLines of widget
int maxLines = 2;
// and here is the TextPainter declaration
TextPainter textPainterExample = TextPainter(
text: TextSpan(
text: text,
),
textDirection: TextDirection.ltr,
maxLines: maxLines,
);
// we simulate the painting of that text and get registered information about it such as offsets...
textPainterExample.layout(maxWidth: width);
// and this is the index of the letter which starts overflowing on
final indexOfOverflowing = textPainterExample.getPositionForOffset(Offset(width, 0)).offset;
2条答案
按热度按时间7jmck4yq1#
rsaldnfx2#
你可以使用
TextPainter
来完成这个任务,它允许你单独绘制一个文本,并且它负责绘制Text
小部件:现在你已经得到了文本开始溢出的
indexOfOverflowing
,你可以简单地substring
它,如下所示:现在您可以使用
limitedText
。