flutter计算文本内容行数

2023-02-21  本文已影响0人  CodeLuck
LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
        final span = TextSpan(
            text: _textFieldController.text,
            style: TextStyle(color: Color(0xff222222), fontSize: 28.w));
        final tp = TextPainter(
            text: span, maxLines: 2, textDirection: TextDirection.ltr);
        tp.layout(maxWidth: constraints.maxWidth);
        List aaa = tp.computeLineMetrics();
        TextSelection selection = TextSelection(
            baseOffset: 0, extentOffset: _textFieldController.text.length);

        List<TextBox> boxes = tp.getBoxesForSelection(selection);
        int numberOfLines = boxes.length;
        print('lines:=======  ==   ${aaa.length}  === $numberOfLines');
   }
)

上一篇 下一篇

猜你喜欢

热点阅读