Styling Text
Text can be included in your game using PIXI’s Text
, BitmapText
, or HTMLText
classes. Which styling options
are available will depend on the class you choose.
Text
The Text
class is the most basic way to include text in your game.
Text elements are styled using the style
property which can reference a locally installed font family or a web font.
Web Fonts
To use a web font in your game, you can use a service like Google Fonts or
Adobe fonts to find the fonts you want to include and grab the @import
URL
to include it in your CSS file or in your game’s index.html
file:
To use the font, include it in the style
property of a text element in your game:
BitmapText
Bitmap text elements use PIXI’s BitmapText
class and can be styled using bitmap fonts that you’ve
already loaded. For example:
HTMLText
HTML text elements can be styled using HTML tags. For example:
To style HTML text, pass a style
property to the htmlText
method:
If the user has Helvetica installed locally it will render correctly, but if not it will fall back to a default font. To ensure it loads correctly, we can preload the font and use that to apply the style.
Custom Fonts
To load a custom font and apply it to an HTMLText
element we use dill pixel’s
loadAndAddHTMLTextStyle
method to create the styles and load the individual
font files for the different weights. Then we can retrieve the style
using getHTMLTextStyle
and apply it to the HTMLText
element.