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 BaseTestGameMobileTheme extends UIEditorTheme
{
    [Embed(source="/../../theme/assets/fonts/kaiti.ttf",fontFamily="kaiti",mimeType="application/x-font",embedAsCFF="false")]
    protected static const kaiti:Class;
 
    ...
 
    public function BaseTestGameMobileTheme(scaleToDPI:Boolean = true, themeMediator:IUIEditorThemeMediator = null)
    {
        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: 2015/12/15 20:51 by johannh
 
Powered by DokuWiki