宜昌本地软件开发与弱电工程服务商
项目咨询 · 快速响应
您当前的位置: > 博客2 > AS2与AS3技术

[AS3]as3设置文本字体的方法示例

2016-10-13 00:16:48 点击量:9 标签: 收藏本文

使用HTML的标签,或者设置TextFormat对象的font属性,或者通过CSS的font-family属性
修改字体有多种方法,如果使用HTML的话可通过 标签更改:field.htmlText = "Formatted text";
也可设置TextFormat对象的font属性:formatter.font = "Arial";
或者在CSS中定义font-family 属性:p {font-family: Arial;}
需要注意的是电脑中必须要有你所指定的字体,因为有些电脑上可能没有安装相应的字体,这是可指定多种字体:formatter.font = "Arial, Verdana, Helvetica";
如果都没有指定字体,默认使用系统字体。
另外我们还可使用字体组,字体组是系统默认字体的一个分类,有三种: _sans, _serif, 和_typewriter。
_sans 组包含如Arial 或Helvetica,_serif组包含如Times 或Times New Roman,_typewriter 组包含如Courier 或Courier New
嵌入字体
通过[embed]元数据嵌入字体,设置文本框的embedFonts属性为true,通过标签,TextFormat对象或CSS应用字体
嵌入系统字体:
[Embed(systemFont="Onyx",fontName="hxw",mimeType="application/x-font-truetype")]
嵌入非系统字体:
[Embed(source="xjlFont.fon",fontName="xjl",mimeType="application/x-font")]
可以设置textField.rotation = 30来设置文字旋转(放在定时器或OnEnterFrame事件里),文字必须为嵌入字体,否则无法正常显示。 

TextFormat方式
  1. package { 
  2.  import flash.display.Sprite; 
  3.  import flash.text.TextField; 
  4.  import flash.text.TextFieldAutoSize; 
  5.  import flash.text.TextFormat; 
  6.  
  7.  [Embed(source="xjlFont.fon",fontName="xjl",mimeType="application/x-font-truetype")] 
  8.  public class Sample0410 extends Sprite 
  9.  { 
  10.   public function Sample0410() 
  11.   {  
  12.    var textBox:TextField = new TextField(); 
  13.    textBox.text = "hello everybody,my name  is cuplayer.com"
  14.    textBox.autoSize = TextFieldAutoSize.CENTER; 
  15.    textBox.embedFonts=true
  16.    addChild(textBox); 
  17.    var formatter:TextFormat = new TextFormat(); 
  18.    formatter.font = "xjl"
  19.    formatter.size = 30
  20.    textBox.setTextFormat(formatter); 
  21.   } 
  22.  } 

CSS方式

  1. package { 
  2.  import flash.display.Sprite; 
  3.  import flash.text.StyleSheet; 
  4.  import flash.text.TextField; 
  5.  import flash.text.TextFieldAutoSize; 
  6.  [Embed(source="xjlFont.fon",fontName="xjl",mimeType="application/x-font-truetype")] 
  7.  public class Sample0410 extends Sprite 
  8.  { 
  9.   public function Sample0410() 
  10.   {  
  11.    var css:StyleSheet = new StyleSheet(); 
  12.    var styleObj:Object = {color:"#FFFF00", fontFamily:"xjl",fontSize:"30px"}; 
  13.    css.setStyle(".stdStyle",styleObj); 
  14.     
  15.    var textBox:TextField = new TextField(); 
  16.    textBox.styleSheet = css
  17.    textBox.text = "<span class='stdStyle'>hello everybody,my name  is  老胡</span>"
  18.    textBox.autoSize = TextFieldAutoSize.CENTER; 
  19.    textBox.embedFonts=true
  20.    addChild(textBox); 
  21.   } 
  22.  } 

 

ABOUT US
宜昌农人网络科技有限公司

我们是一家提供综合软件外包与弱电工程服务的技术公司。

业务涵盖定制软件、管理系统、网站、小程序、APP、系统集成、网络布线、监控安防、门禁、机房建设及长期技术维护。

公司地址:湖北省宜昌市伍家岗区东站二路6号三峡(宜昌)大数据产业园海洋馆二楼A203室