使用AS3中的FTE來渲染文本(FLash文本引擎入門)

來源:互聯網
上載者:User

最近很多開發
者都在關注手機遊戲
的開發,那麼我來看看一個新特性(FP10),使用這種方法可以很好的解決跨硬體的問題.

FTE (Flash文本引擎)

FTE 提供對文本度量、格式和雙向文本的複雜控制的低層級支援。設計 FTE 的主要目的在於為開發人員建立文本處理組件
提供基礎。 在下面簡單的例子中,你將會發現使用FTE並不複雜.

下面
代碼
需要FP10


 package {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.text.engine.*;
    public class fte extends Sprite {
        public function fte():void {
            // FTE TEXT
            var font_description:FontDescription = new FontDescription();
            font_description.fontName="Verdana";
            font_description.fontWeight=FontWeight.BOLD;
            font_description.fontPosture=FontPosture.ITALIC;
            font_description.renderingMode=RenderingMode.CFF;
            font_description.locked=true;
            var element_format:ElementFormat=new ElementFormat(font_description);
            element_format.fontSize=40;
            element_format.kerning=Kerning.ON;
            element_format.color=0x000000;
            element_format.alpha=1;
            var text_element:TextElement=new TextElement("Hello Flash World",element_format);
            var text_block:TextBlock = new TextBlock();
            text_block.content=text_element;
            var text_line:TextLine=text_block.createTextLine(null,500);
            text_line.x=20;
            text_line.y=40;
            addChild(text_line);
            // NORMAL TEXT
            var text_field:TextField=new TextField();
            var text_format:TextFormat = new TextFormat();
            text_format.color=0x000000;
            text_format.size=40;
            text_format.bold=true;
            text_format.font="Verdana";
            text_format.italic=true;
            text_field.x=18;
            text_field.y=30;
            text_field.width=460;
            text_field.height=60;
            text_field.text="Hello Flash World";
            text_field.setTextFormat(text_format);
            addChild(text_field);
        }
    }
}


1 至5 行:
匯入所需類庫.

9至14行:

使用
FontDescription

類來定義字型.通過鎖定(第14行)字型和設定渲染模式為CFF(第13行)來增強文本的可讀性,在小字型大小的情況下效果
更為明顯,相關說明可以在官方文檔中看到.

15至19行:

使用
ElementFormat

類來設定格式資訊.但是我並沒有使用文檔中提到的基準對齊的新特性,關閉字元間距調整會提升文本的可讀性.

20行:

使用
TextElement

類來表示我想顯示並且已設定好格式的文本字串,這塊並沒有什麼特別的.

21至22行:

TextBlock

類會自己渲染自己,但是其更多功能是關于格式和控制.

23至25行:


使用TextLine

類來顯示內容.如文檔中所描述的一樣,它僅僅是有很多隻讀的屬性
.

23至40行:

和普通的文字框一樣的操作

我們的成果是:

下載

(12.23 KB)昨天 01:21

上面的一行是使用FTE,下面的是使用普通文字框.
我不得不承認:其實二者並沒有什麼顯著區別(在本例中).
  這個例子的作用僅僅是幫你入門FTE

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.