flash.text.engine 入门笔记(二)

回顾一下flash.text.TextField这个类,在图文混排的时候存在什么缺陷?

相信很多人都认为其结构描述能力太差,图片不支持inline,而且要更换文本域内的图片也会显得非常麻烦(虽然flash.text.engine也不见得简单)。

首先介绍下GraphicElement类,此类顾名思义就是一个图像元素,纯属一个表现型的元素,相当于HTML中的IMG,只是其不具备读取图像功能,只能通过Loader将外部图片读取,然后把Loader作为其Content。

看看如下实例代码:

//绘制一个红色矩形来作为我们的图片

var shape:Shape = new Shape();

shape.graphics.beginFill(0xff0000);

shape.graphics.drawRect(0, 0, 100, 100);

shape.graphics.endFill();

//建立一个图像元素,并将我们的shape作为其显示的内容,同时也可以成为一个Loader

var graphicEle:GraphicElement = new GraphicElement(shape, shape.width, shape.height);

//建立一个文本块,用于装载图像元素并输出行

var textBlock:TextBlock = new TextBlock();

textBlock.content = graphicEle;

var textLine:TextLine = textBlock.createTextLine(null, 200);

textLine.x = 100;

textLine.y = 100;

addChild(textLine);

上面的例子已经简单介绍了GraphicElement类的使用方法,那么如何实现图片inline的图文混排呢?在flash.text.engine里提供了一个GroupElement类,通过其能将所有的元素组合起来,再看如下代码:

//绘制一个红色矩形来作为我们的图片

var shape:Shape = new Shape();

shape.graphics.beginFill(0xff0000);

shape.graphics.drawRect(0, 0, 100, 100);

shape.graphics.endFill();

//建立一个图像元素,并将我们的shape作为其显示的内容,同时也可以成为一个Loader

var graphicEle:GraphicElement = new GraphicElement(shape, shape.width, shape.height);

//建立一个文本元素

var format:ElementFormat = new ElementFormat(new FontDescription("Arial"));

format.fontSize = 12;

var textEle:TextElement = new TextElement("Hello World!", format);

//把所有刚刚建立的元素丢到一个Vector里面,并且组合成一个组元素

var eleVector:Vector.<ContentElement> = new Vector.<ContentElement>();

eleVector.push(textEle, graphicEle);

var groupEle:GroupElement = new GroupElement(eleVector);

//建立一个文本块,用于装载图像元素并输出行

var textBlock:TextBlock = new TextBlock();

textBlock.content = groupEle;

var textLine:TextLine = textBlock.createTextLine(null, 300);

textLine.x = 100;

textLine.y = 100;

addChild(textLine);

通过如上的代码,各位看官应该能对flash.text.engine有了个大体的认识了吧,至于排版的细节,相信也不需要小弟在这里赘述了,因为textline更搞定一切,加上小弟才疏学浅,还不如各位看官直接查阅帮助文档的属性来得有效。

loading...