Font asset not working

I try to display a simple textfield with an embeded font on flash target but this do not work and I really don’t understand why…
I took the same font as the one used in PiratePig to be sure this was not an issue related to my font. Here is the code I use:

var font:Font = Assets.getFont("fonts/FreebooterUpdated.ttf");
var fformat:TextFormat = new TextFormat(font.fontName, 60, 0xFF0000);
fformat.align = TextFormatAlign.CENTER;
var firstLetter:TextField = new TextField();
firstLetter.text = "T"; 
firstLetter.selectable = false;
firstLetter.defaultTextFormat = fformat;
firstLetter.embedFonts = true;

the trace display “Freebooter” so the font asset is correctly found. But my textfield do not display at all…
Now, if I comment the firstLetter.embedFonts = true; line, I can see my textfield but all the textformat parametters are ignored (it uses a font that is not Freebooter, with a size that is something like 12 and is black rather than red…)
What drives me crazy is that the piratepig example just work fine and I really don’t understand what difference my code and the piratepig one have that makes one to work and the other not…

Did you add the <assets /> tag to project.xml?

Yes sure. In my project.xml file I have:

<assets path="Assets" rename="assets" />
<assets path="Assets/fonts" rename="fonts" include="*.ttf" />
<assets path="Assets/images" rename="images" include="*" exclude="icon.svg" />

But, as said, the font asset is correctly found as the trace call correctly output the font name. So I doubt the issue is linked to the asset declaration.

Oops, I guess I didn’t pay close enough attention to your post.

Have you tried putting “firstLetter.defaultTextFormat = fformat” before “firstletter.text = "T"”?

1 Like

No I haven’t but I just did now and it works ;). Thanks. That solved my problem.

Remember, defaultTextFormat controls the format of anything added afterwards. setTextFormat() changes the format of text that’s already there.

On android, this isn’t working. The font used is not the one defined in myTextFormat… (but the size and color is correct)
On flash and windows everything is OK though
I am using latest versions of lime (2.7.0) and openfl (3.4.0).

Can you double check, if you importing flash.text.TextField or Openfl.text.TextField and not anything else.

openfl=> 3.0.8
swf => 1.8.7

Here is my code, that runs fine at my end :

<?xml version="1.0" encoding="utf-8"?>
    <meta title="Website" package="myapp.something.something" version="1.0.0" company="OpenFL Technologies" />
    <app main="myapp.Main" path="Export" file="Main" />
    < window width="640" height="480" if="flash" />
    <window width="640" height="480" if="html5" />
    <source path="Source" />
    <haxelib name="openfl" />
    <assets path="Assets" rename="assets" exclude="*.svg" />
    <assets path="Assets/fonts" rename="fonts" include="*.ttf" />     
    <icon path="Assets/openfl.svg" />
    <haxelib name="swf" />

and here is the code for textfield

 package myapp ;

import flash.text.*;
import flash.display.*;
import flash.Lib;
import openfl.Assets;

class Main extends Sprite 
    private var myText_Tf:TextField;
    public function new () 

         super ();
        myText_Tf = new TextField();
        addTextField(myText_Tf, "example text example text", 20,357, 249,0x000000);
    private function addTextField(tf:TextField,label_param_str:String,size_param_int:Int, x_param_int:Int,y_param_int:Int,color_uint:UInt ):Void 
        var font = Assets.getFont ("assets/fonts/verdana.ttf");
        var defaultFormat = new TextFormat (font.fontName, size_param_int, color_uint);
        //tf = new TextField();
         tf.defaultTextFormat = defaultFormat;
        tf.x = x_param_int; 
        tf.y = y_param_int; 
        tf.text = label_param_str ;
        tf.width = 400;
        tf.height= 50;
        tf.selectable = false;
        tf.multiline = true ;
        tf.wordWrap = true ;
        tf.border = true ;
          tf.embedFonts = true;


Definitively not working on android (of course I replaced verdana.ttf by my own font because I haven’t verdana.ttf in my assets/fonts directory. Anyway, verdana is pretty much the default font used by openfl or really similar to it. So you should use a font that can be easily identified (as a calligraphy font) to test and check it works…).
I also tested the code on Flash just to be sure it was OK and the font file was correctly found and imported. On flash it works fine.