如何在颤动中测量文本

时间:2019-05-28 05:39:16

标签: flutter

在android中,我可以这样做。
val paint=Paint()
paint.textSize=16F;

//Return the number of chars that the measured width exceeds maxWidth
val count=paint.breakText("hello world",true,100F,null);

//get width of the text
paint.measureText("hello world");

来自52659759的答案

扑朔迷离,可以做到这一点。

final render = RenderParagraph(TextSpan(text: "hello world",style: TextStyle(fontSize: 16)),textDirection: TextDirection.ltr);
render.layout(BoxConstraints());

//Return the number of chars that the measured width exceeds maxWidth
final position=render.getPositionForOffset(Offset(100, 0));

//get width of the text
final offset=render.getOffsetForCaret(position,Rect.largest); 

0 个答案:

没有答案