Important changes: Loading fonts just got easier!

Important changes: Loading fonts just got easier!

With the announcement of Public Release 2830 comes changes to how fonts are loaded/displayed.

Previously, the usage of custom fonts was dependent on the operating system and the type of font being used. Sometimes you would need to use a font’s file name and other times you would use the “internal font name.”  Many developers made things easier on themselves by ensuring the font file name and the internal font name matched.

Now, you can simply use the font file name + extension. This is consistent across all platforms except Windows Phone 8.

For example, to display a custom font where the file name is superfont.ttf, simply use:

Windows Phone 8 still has a different requirement which is "#". Example:

To simplify working with fonts, you should adopt this new method of loading custom fonts moving forward.

Tags:
,
Rob Miracle
rob@coronalabs.com

Rob Miracle creates mobile apps for his own enjoyment and the amusement of others. He serves the Corona Community in the forums, on the blog, and at local events.

19 Comments
  • Jason Schroeder
    Posted at 14:43h, 25 February

    Best part of this new feature: we can put all our fonts in a subfolder and reference them thusly:
    local myText = display.newText( "Hello World!", 100, 200, "fotns/superfont.ttf", 16 )
    My nice tidy project folders thank you for this @Rob!

  • Kevin Bradford
    Posted at 18:00h, 25 February

    Great change, thank you! Fonts have always been such a headache to use cross-platform.

  • Mario
    Posted at 07:35h, 26 February

    This’ll help out a lot!!

    -Mario

  • jch_APPLE
    Posted at 04:59h, 27 February

    Is it a first step to better text handling ? Anyway it’s a great change, thanks !

  • Scott Harrison
    Posted at 10:26h, 28 February

    Can you use fonts in documents directory?

    • Rob Miracle
      Posted at 16:44h, 28 February

      No. It’s just a relative path to the font file from system.ResourceDirectory.

    • Julius Bangert
      Posted at 10:48h, 17 May

      Is there no way to reference fonts from the documents directory? It would be great to be able to download a new font to use with new content as we go?

  • demo
    Posted at 11:52h, 29 February

    Great news. Now, can we use fonts such as font-awesome for Logo etc. instead of creating separate images for each resolution, or is that still not supported?

    • Rob Miracle
      Posted at 14:55h, 29 February

      You can still only use TTF and OTF fonts.

  • Crossman
    Posted at 12:03h, 03 March

    Do you still have to name the fonts in the config files?

  • Dave Z
    Posted at 13:13h, 03 March

    Using Daily Build 2823, the following line:

    TitleTxt = display.newText( “This is the title”, _W/2,_H/2, “titanone.ttf”, 28 )

    Generates an error: “WARNING: Could not load font titanone.ttf.ttf. Using default font.”

    What’s the deal???

    • Dave Z
      Posted at 13:17h, 03 March

      I meant Build 2832…

    • Dave Z
      Posted at 13:19h, 03 March

      Haha! Never mind. It works! Sometimes you miss the silliest things!

  • Ed
    Posted at 20:13h, 07 March

    Will the old way continue to work or is this the new way going forward?

    • Rob Miracle
      Posted at 15:43h, 08 March

      It should.

    • Rob Miracle
      Posted at 18:22h, 08 March

      It should

  • Yanko Popov
    Posted at 02:29h, 25 April

    While the new way of loading fonts is indeed easier, the old way seems much faster performance-wise, especially if you load a lot of text objects in the same time. It seems that the new way loads the font file provided in the code each time when a new text object is created, which can be inefficient, and leads to pretty significant performance stutters IF you have a big number of text objects created in the same time. Therefore I think it is important to leave the old way of specifying custom fonts somewhere in the API documentation as well as the new one.

    • Rob Miracle
      Posted at 07:34h, 25 April

      Yes, loading by filename is slower. That’s why we let you still use the font family name as a method.

      • Yanko Popov
        Posted at 05:18h, 02 May

        Thanks! My bad, I just completely missed that in the API documentation 🙂