美文网首页
求Flutter中文本的宽度和高度

求Flutter中文本的宽度和高度

作者: 杨闯 | 来源:发表于2020-04-30 23:00 被阅读0次

在使用过程中,会有计算文本的宽度高度的功能,那么在flutter的时候也是比较常用的,这也是我曾经纠结好久的一个问题。

 double paintWidthWithTextStyle(TextStyle style) {
    final TextPainter textPainter = TextPainter(
                       text: TextSpan(text: this, style: style), 
                     maxLines: 1,
              textDirection: TextDirection.ltr)
      ..layout(minWidth: 0, maxWidth: double.infinity);
    return textPainter.size.width;
  }

此时的this是String,就可以算出对应的宽度,自然而然也就知道了对应的高度。

相关文章

网友评论

      本文标题:求Flutter中文本的宽度和高度

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