回顧一下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更搞定一切,加上小弟才疏學淺,還不如各位看官直接查閱協助文檔的屬性來得有效。