美文网首页
flutter计算文本内容行数

flutter计算文本内容行数

作者: CodeLuck | 来源:发表于2023-02-21 16:44 被阅读0次
    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');
       }
    )
    
    

    相关文章

      网友评论

          本文标题:flutter计算文本内容行数

          本文链接:https://www.haomeiwen.com/subject/vifokdtx.html