Search code examples
javalibgdxnine-patch

Libgdx nine patch text button height issue


I am using a TextButton to create a dialog bubble effect in my game. I have created the nine patch file as on the picture enter image description here everything seems to be fine until I try to use this bubble.9 with LibGDX, the TextButton is not stretching correctly based on the text height.

enter image description here

enter image description here

You can see that a multilines text extends the TextButton height but not correctly.

Here is the code

Load the bitmap font

FileHandleResolver resolver = new InternalFileHandleResolver();
assetManager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
assetManager.setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(resolver));
FreetypeFontLoader.FreeTypeFontLoaderParameter dialogFont = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
dialogFont.fontFileName = Conf.ASSET_FONT;
dialogFont.fontParameters.size = 8;

Initialize the TextButtonStyle

NinePatch patch = atlas.createPatch("bubble");
NinePatchDrawable dialogBoxPatch = new NinePatchDrawable(patch);
buttonStyle = new TextButton.TextButtonStyle();
buttonStyle.up = dialogBoxPatch;
buttonStyle.down = dialogBoxPatch;
buttonStyle.checked = dialogBoxPatch;
buttonStyle.over = dialogBoxPatch;
buttonStyle.font = dialogFont;
buttonStyle.fontColor = Color.BLACK;

actual rendering

dialogBox = new TextButton("some\ntext", buttonStyle);
dialogBox.draw(batch, 1);

Does someone see what the problem is?


Solution

  • I finally found the problem. I have decided to test the ninepatch with another font and it worked like a charm. To understand what was going on, I have opened the two fonts with glyphrstudio. It appears that the problematic font is not correctly designed.

    Bad font

    enter image description here

    Good font

    enter image description here

    You can see that the base line and capheight are not correctly setted on the bad font.

    I hope this will help other people facing the same problem.