# Resize font when screen resizes desktop Libgdx

I create my font using the following code:

public BitmapFont createFont(FreeTypeFontGenerator ftfg, float dp)
{
FreeTypeFontParameter f = new FreeTypeFontParameter();
f.size = (int)(dp * Gdx.graphics.getDensity());
f.color = Color.BLACK;
f.minFilter = Texture.TextureFilter.Nearest;
f.magFilter = Texture.TextureFilter.MipMapLinearNearest;
ftfg.scaleForPixelHeight((int)(dp * Gdx.graphics.getDensity()));
return ftfg.generateFont(f);
}

myFont = createFont(new FreeTypeFontGenerator(Gdx.files.internal("Fonts/Roboto-Black.ttf")), 16);


I have some labels to which I set the font using:

Label myLabel= new Label("Text", uiSkin);
myLabel.setWrap(true);
myLabel.getStyle().font = myFont;


How I create the stage:

Stage collectionStage = new Stage(new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getWidth()));


I also tried with no viewport or with other viewports.

In my resize method:

public void resize(int width, int height) {
Viewport viewport = collectionStage.getViewport();
viewport.update(width, height, false);
viewport.apply();
}


The problem is that when I resize the window on Desktop the font gets distorted. Do I have to create another font in the resize method, and then for each label that I have, reassign the new font? Or should I do something else?

• Distorted as in pixelated or as in stretched / other? – Charanor Jun 20 '17 at 14:20
• Also, if you use a stretch viewport it will stretch your screen and everything in it. Try using a FitViepwort or an ExtendViewport instead that keeps your aspect ratio. – Charanor Jun 20 '17 at 15:01

Saw because of bump,

If it comes out blurry than you are most likely using a font that is too small for the resolution you are working at. What you'll want to do is to create a font at the size that is suggested for that font, and then scale it up afterwords.

Ex:

fontGenerator = new FreeTypeFontGenerator(Gdx.files.internal("Fonts/pixels.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter fontParameter;
fontParameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
fontParameter.size = 16;
fontParameter.color = Color.WHITE;
font = fontGenerator.generateFont(fontParameter);
// scale here
font.getData().setScale(/*however much you want to scale with*/);


Hope this helps.

You need to use a Viewport which keeps the aspect ratio, for example a FillViewport. This will handle resizing for you, so you "only" need to manage the position. Also, I'd not scale the fonts. Create some fonts in different sizes at the beginning and use them.

• I tried to use some of the available viewports, but the fonts still get distorted on resize. – Andy Mar 24 '17 at 21:39
• Can you upload the code of how you initialize and use the viewport? – AlGrande Mar 24 '17 at 22:10
• I updated my question with a bit more code. – Andy Mar 24 '17 at 22:31