问题
First, I loaded the skin as a json file. In the file, I put two BitmapFont defintions ("font" and "big-font") and left them practically blank (I did load a dummy ".fnt" file to prevent the parser from complaining). I then generated two FreeType Fonts using the following code:
//start font generator
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/button-font.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameters = new FreeTypeFontGenerator.FreeTypeFontParameter();
//default font (the sizes are arbitrary, since it did not work...)
parameters.size = 10;
BitmapFont defaultFont = generator.generateFont(parameters);
//big font
parameters.size = 100;
BitmapFont bigFont = generator.generateFont(parameters);
And set them as the skin fonts like so:
skin.add("font", defaultFont, BitmapFont.class);
skin.add("big-font", bigFont, BitmapFont.class);
The problem is that the gui (particularly the TextButton, which uses the "font" font) does not seem to care about what I did programmatically and keeps using the dummy font that I've set in the json file.
What am I doing wrong? Is there a better way to integrate ttf fonts and json skin parsing?
Thanks in advance for any help.
回答1:
Follow these steps :
First, create your font from a TTF file
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/button-font.ttf")); FreeTypeFontParameter parameter = new FreeTypeFontParameter(); parameter.size = 10; BitmapFont defaultFont = generator.generateFont(parameter); generator.dispose(); // don't forget to dispose to avoid memory leaks!
Create a blank skin using default constructor and add your font to the skin.
Skin skin = new Skin(); skin.add("font",defaultFont);
Add your atlas file to your skin.
skin.addRegion(new TextureAtlas(Gdx.files.internal("mySkin.atlas")));
Load your Json file to your skin.
skin.load(Gdx.files.internal("mySkin.json"));
then, in your JSON file you can reference “font”
{ font: font }
来源:https://stackoverflow.com/questions/45523878/libgdx-skin-not-updating-when-changing-font-programmatically