How to embed a ttf font

In order to load a ttf font, you can’t just drop the ttf files to AssetManager, you need to compile it into a swf. It’s recommended to do it inside your custom theme. For more information about how custom theme works see here

Open your game theme file and embed the font as following. In order to make starling TextField working, make sure to set embedAsCFF=“false”. You also need to manually register the font in the constructor.

public class BaseMetalWorksMobileTheme extends StyleNameFunctionTheme
{
    [Embed(source="/../../theme/assets/fonts/kaiti.ttf",fontFamily="kaiti",mimeType="application/x-font",embedAsCFF="false")]
    protected static const kaiti:Class;
 
    ...
 
    public function BaseMetalWorksMobileTheme()
    {
        Font.registerFont(kaiti);
 
        ...
    }
 
    ...
}

Compile EmbeddedTheme.swf and drop it inside YOUR_WORKSPACE/libs and reload the editor.

Now you can test it by adding a starling TextField and set the fontName to the name of your new font. (Note: true type fonts are not showed from the drop down, you need to manually type in the name.)

  builder/ttf_font.txt · Last modified: 2018/02/07 05:20 by johannh
 
Powered by DokuWiki