flash.text.engine 入門筆記(二)

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.