问题描述
有什么方法可以仅使用 ActionScript 在 DisplayObject 或 Shape 中绘制文本?我在网上能找到的唯一方法是创建一个 TextField,但我无法将 TF 添加到 DisplayObject 或 Shape.
已解决,感谢 viatropos.>
对于任何感兴趣的人:
DisplayObject
实现了 IBitmapDrawable
,它可以作为参数传递给 BitmapData
对象的 draw
函数,然后可以使用 graphics.beginBitmapFill
绘制.
var textfield:TextField = new TextField;textfield.text = "文本";var bitmapdata:BitmapData = new BitmapData(theWidth, theHeight, true, 0x00000000);位图数据.绘制(文本字段);graphics.beginBitmapFill(位图数据);graphics.drawRect(0, 0, theWidth, theHeight);graphics.endFill();
好问题.这超出了我曾经需要做的任何事情,但我想我知道该怎么做.
Shape 扩展 DisplayObject,但不扩展 DisplayObjectContainer,因此您无法向其添加任何内容.但是它确实具有graphics
属性,因此您可以在其中绘制内容.我能想到的最好方法是拍摄 TextField 的 Bitmap 快照,并将其绘制到 Shape 中.我知道这就是 Degrafa 为他们的 RasterText 所做的(查看 来源,真的很有帮助).
如果您将 Shape 改为 Sprite,那就容易多了.Sprite 扩展了 DisplayObjectContainer,所以你可以在那里添加你的 TextField.
希望有所帮助,兰斯
Is there any way to draw text in a DisplayObject or Shape using only ActionScript? The only way I can find on the web is by creating a TextField, but I can't add a TF to a DisplayObject or Shape.
Edit:
Solved thanks to viatropos.
For anyone that's interested:
DisplayObject
implements IBitmapDrawable
which can be passed as an argument to the draw
function of a BitmapData
object, which then can be drawn using graphics.beginBitmapFill
.
var textfield:TextField = new TextField;
textfield.text = "text";
var bitmapdata:BitmapData = new BitmapData(theWidth, theHeight, true, 0x00000000);
bitmapdata.draw(textfield);
graphics.beginBitmapFill(bitmapdata);
graphics.drawRect(0, 0, theWidth, theHeight);
graphics.endFill();
Good question. This is beyond anything I've ever needed to do, but I think I know how to do it.
Shape extends DisplayObject, but not DisplayObjectContainer, so you can't add anything to it. But it does have the graphics
property, so you can draw things into it. The best way I can think of is to take a Bitmap snapshot of the TextField, and draw that into the Shape. I know this is what Degrafa does for their RasterText (check out the source, it's really helpful).
If you changed your Shape to a Sprite instead, it's a lot easier. Sprite extends DisplayObjectContainer, so you could add your TextField there.
Hope that helps,Lance
这篇关于在 ActionScript 3 中在形状上绘制文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!