25 February 2016
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:
1 2 |
local myText = display.newText( "Hello World!", 100, 200, "superfont.ttf", 16 ) |
Windows Phone 8 still has a different requirement which is "#"
. Example:
1 2 |
local myText = display.newText( "Hello World!", 100, 200, "superfont.ttf#Super Font Regular", 16 ) |
To simplify working with fonts, you should adopt this new method of loading custom fonts moving forward.
Jason Schroeder
Posted at 14:43h, 25 FebruaryBest 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 FebruaryGreat change, thank you! Fonts have always been such a headache to use cross-platform.
Mario
Posted at 07:35h, 26 FebruaryThis’ll help out a lot!!
-Mario
jch_APPLE
Posted at 04:59h, 27 FebruaryIs it a first step to better text handling ? Anyway it’s a great change, thanks !
Scott Harrison
Posted at 10:26h, 28 FebruaryCan you use fonts in documents directory?
Rob Miracle
Posted at 16:44h, 28 FebruaryNo. It’s just a relative path to the font file from system.ResourceDirectory.
Julius Bangert
Posted at 10:48h, 17 MayIs 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 FebruaryGreat 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 FebruaryYou can still only use TTF and OTF fonts.
Crossman
Posted at 12:03h, 03 MarchDo you still have to name the fonts in the config files?
Dave Z
Posted at 13:13h, 03 MarchUsing 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 MarchI meant Build 2832…
Dave Z
Posted at 13:19h, 03 MarchHaha! Never mind. It works! Sometimes you miss the silliest things!
Ed
Posted at 20:13h, 07 MarchWill the old way continue to work or is this the new way going forward?
Rob Miracle
Posted at 15:43h, 08 MarchIt should.
Rob Miracle
Posted at 18:22h, 08 MarchIt should
Yanko Popov
Posted at 02:29h, 25 AprilWhile 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 AprilYes, 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 MayThanks! My bad, I just completely missed that in the API documentation 🙂