如何使用TextPainter绘制文本? [英] How do you use a TextPainter to draw text?

查看:425
本文介绍了如何使用TextPainter绘制文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关CustomPainter绘制方法的文档说,要绘制文本在画布上,使用 TextPainter 。因此,在我的 MyCustomPainter 的绘制方法中,我具有以下内容:

The documentation for CustomPainter's paint method says, "To paint text on a Canvas, use a TextPainter". So within my MyCustomPainter's paint method I have the following:

TextSpan span = new TextSpan(text: 'Yrfc');
TextPainter tp = new TextPainter(text: span, textAlign: TextAlign.left);
tp.layout();
tp.paint(canvas, new Offset(5.0, 5.0));

我尝试了各种偏移量(Offset.zero,Offset。无限,新的偏移量(10.0,10.0),但我始终看不到屏幕上绘制的文本。

I've tried a variety of offsets (Offset.zero, Offset.infinite, new Offset(10.0, 10.0) but I never am able to see the text painted on the screen.

推荐答案

我在输入此问题时找到了答案,但是现在我已经在努力角力了一段时间,因此发布以防万一,可以帮助其他人。

I found the answer as I was typing up this question but I've been wrestling with it for a while now, so posting in case it helps anyone else.

解决方案是将TextSpan行更改为:

What solved it was changing the TextSpan line to:

TextSpan span = new TextSpan(style:new TextStyle(color:Colors.grey [600]),文本: Yrfc);

显然,它是无形地绘制文本,还是自白色(背景)颜色绘制我没有明确选择颜色。

Apparently it was either drawing the text invisibly or as white (background) color since I hadn't made my color choice explicit.

这篇关于如何使用TextPainter绘制文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆