Flash / Flex / ActionScript/TextField — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 08:14, 26 мая 2010

Automatically selecting a fallback font

 
package {
  import flash.display.*;
  import flash.text.*;
  public class Main extends Sprite {
    public function Main() {
      var format:TextFormat = new TextFormat(  );
      format.font = getFont(["ZapfChancery", "Verdana", "Arial", "_sans"]);
      var t:TextField = new TextField(  );
      t.text = "ActionScript is fun!";
      t.autoSize = TextFieldAutoSize.LEFT;
      t.setTextFormat(format)
      addChild(t);
    }
    public function getFont (fontList: Array):String {
      var availableFonts:Array = Font.enumerateFonts(true);
      for (var i:int = 0; i < fontList.length; i++) {
        for (var j:int = 0; j < availableFonts.length; j++) {
          if (fontList[i] == Font(availableFonts[j]).fontName) {
            return fontList[i];
          }
        }
      }
      return null;
    }
  }
}



Class-level font embedding

 
package {
  import flash.display.*;
  import flash.text.*;
  import mx.core.FontAsset;
  
  public class HelloWorld extends Sprite {
    Verdana;
    public function HelloWorld (  ) {
      var t:TextField = new TextField(  );
      t.embedFonts = true;
      t.htmlText = "<FONT FACE="Verdana">Hello world</FONT>";
      addChild(t);
    }
  }
}
[Embed(source="c:/windows/fonts/verdana.ttf", fontFamily="Verdana")]
class Verdana extends FontAsset {
}



Creates a TextField object and then tells Flash Player to render it with embedded fonts using the FlashType renderer:

 
package{
  import flash.display.Sprite;
    import flash.text.*;  
  public class Main extends Sprite{
    public function Main(){
        var t:TextField = new TextField(  );
        t.embedFonts = true;
        t.antiAliasType = AntiAliasType.ADVANCED;
    }
  }
}



Determining Glyph Availability

 
package{
  import flash.display.Sprite;
  import flash.text.*;
  
  public class Main extends Sprite{
    public function Main(){
        var fontName:String = "Verdana";
        var font:Font;
        var fonts:Array = Font.enumerateFonts(true);
        for (var i:int = 0; i < fonts.length; i++) {
          if (fonts[i].fontName == fontName) {
            font = fonts[i];
            break;
          }
        }
        trace(font.hasGlyphs("Hello world"));  // Displays: true
    //    trace(font.hasGlyphs()); // Displays: false
    }
  }
}



Embedded-Text Rendering

 
package{
  import flash.display.Sprite;
  import flash.text.*;  
  public class Main extends Sprite{
    public function Main(){
        var t:TextField = new TextField(  );
        
        t.embedFonts = true;
        t.antiAliasType = AntiAliasType.NORMAL;
        
        addChild(t);
    }
  }
}



Embedding fonts centrally

 
package {
  import flash.display.*;
  import flash.text.*;
  public class Main extends Sprite {
    public function Main (  ) {
      var t:TextField = new TextField(  );
      t.embedFonts = true;
      t.htmlText = "<FONT FACE="Verdana">Hello world</FONT>";
      addChild(t);
    }
  }
}
class FontEmbedder {
    [Embed(source="c:/windows/fonts/verdana.ttf",fontFamily="Verdana")]
    private var verdana:Class;
}



FlashType versus Flash Player"s standard vector-renderer

 
package {
  import flash.display.*;
  import flash.text.*;
  public class FlashTypeDemo extends Sprite {
    [Embed(source="c:/windows/fonts/verdana.ttf",fontFamily="Verdana")]
    private var verdana:Class;
    public function FlashTypeDemo (  ) {
      var t:TextField = new TextField(  );
      t.width = 200;
      t.embedFonts = true;
      t.htmlText = "<FONT FACE="Verdana" SIZE="10">" + "abcdefghijklmnopqrstuvwxyz</FONT>";
      t.antiAliasType = AntiAliasType.ADVANCED;
      addChild(t);
      var t2:TextField = new TextField(  );
      t2.width = 200;
      t2.embedFonts = true;
      t2.htmlText = "<FONT FACE="Verdana" SIZE="10">" + "abcdefghijklmnopqrstuvwxyz</FONT>";
      t2.antiAliasType = AntiAliasType.NORMAL;
      addChild(t2);
      t2.x = 180;
    }
  }
}



Hello World, in Verdana

 
package {
  import flash.display.*;
  import flash.text.*;
  public class Main extends Sprite {
    [Embed(source="c:/windows/fonts/verdana.ttf",
           fontFamily="Verdana")]
    private var verdana:Class;
    public function Main (  ) {
      var t:TextField = new TextField(  );
      t.embedFonts = true;
      t.htmlText = "<FONT FACE="Verdana">Hello world</FONT>";
      addChild(t);
    }
  }
}



Listing all device fonts

 

package{
  import flash.display.Sprite;
  import flash.text.*;  
  public class Main extends Sprite{
    public function Main(){
        var fonts:Array = Font.enumerateFonts(true);
        fonts.sortOn("fontName", Array.CASEINSENSITIVE);
        for (var i:int = 0; i < fonts.length; i++) {
          if (fonts[i].fontType == FontType.DEVICE) {
            trace(fonts[i].fontName + ", " + fonts[i].fontStyle);
          }
        }

    }
  }
}



Listing all embedded and device fonts

 
package{
  import flash.display.Sprite;
    import flash.text.*;
  public class Main extends Sprite{
    public function Main(){
        var fonts:Array = Font.enumerateFonts(true);
        fonts.sortOn("fontName", Array.CASEINSENSITIVE);
        for (var i:int = 0; i < fonts.length; i++) {
            trace(fonts[i].fontType + ": "
                  + fonts[i].fontName + ", " + fonts[i].fontStyle);
        }
    }
  }
}



Listing all embedded fonts

 
package{
  import flash.display.Sprite;
  import flash.text.*;
  public class Main extends Sprite{
    public function Main(){
        var fonts:Array = Font.enumerateFonts(  );
        fonts.sortOn("fontName", Array.CASEINSENSITIVE);
        for (var i:int = 0; i < fonts.length; i++) {
          trace(fonts[i].fontName + ", " + fonts[i].fontStyle);
        }

    }
  }
}



Loading Fonts at Runtime

 
package {
  import flash.display.*;
  import flash.text.*;
  // Embed fonts for use by any .swf file that loads this file
  public class Main extends Sprite {
    [Embed(source="c:/windows/fonts/verdana.ttf",
           fontFamily="Verdana")]
    private var verdana:Class;
    [Embed(source="c:/windows/fonts/verdanab.ttf",
           fontFamily="Verdana",
           fontWeight="bold")]
    private var verdanaBold:Class;
    // Constructor
    public function Main() {
      // Register this class"s embedded fonts in the global font list
      Font.registerFont(verdana);
      Font.registerFont(verdanaBold);
    }
  }
}
  class Fonts extends flash.display.Sprite {
    [Embed(source="c:/windows/fonts/verdana.ttf",
           fontFamily="Verdana")]
    private var verdana:Class;
    [Embed(source="c:/windows/fonts/verdanab.ttf",
           fontFamily="Verdana",
           fontWeight="bold")]
    private var verdanaBold:Class;
  }



Missing Fonts and Glyphs

 
package{
  import flash.display.Sprite;
  import flash.text.*;  
  public class Main extends Sprite{
    public function Main(){
        var t:TextField = new TextField(  );
        t.embedFonts = true;
        t.htmlText = "<FONT FACE="Verdana">Hello <b>world</b></FONT>";
        addChild(t);
    }
  }
}



Set the font with HTML

 
package{
  import flash.display.Sprite;
  import flash.text.*;
  public class Main extends Sprite{
    public function Main(){
        var t:TextField = new TextField(  );
        t.embedFonts = true;
        t.htmlText = "<FONT FACE="fontName">Hello world</FONT>";
    }
  }
}



Using bold and italic with embedded fonts

 
package {
  import flash.display.*;
  import flash.text.*;
  public class Main extends Sprite {
    FontEmbedder;
    public function Main() {
      var t:TextField = new TextField(  );
      t.embedFonts = true;
      t.htmlText = "<FONT FACE="Verdana">Hello <b>world</b></FONT>";
      addChild(t);
    }
  }
}
  class FontEmbedder {
    [Embed(source="c:/windows/fonts/verdana.ttf",
           fontFamily="Verdana")]
    private var verdana:Class;
    [Embed(source="c:/windows/fonts/verdanab.ttf",
           fontFamily="Verdana",
           fontWeight="bold")]
    private var verdanabold:Class;
  }