1

I am new to Libgdx and I have been searching for 3 hours to find a way to change text size in text button but I couldn't find any method for this. What should I do to do this.

 Table rootTable = new Table();
 rootTable.setFillParent(true);
 rootTable.setDebug(true, true);
 Table menuTable = new Table();
 rootTable.add(menuTable).expand().center().right();
 Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));
 TextButton singlePlayer = new TextButton("Single Player",skin);
 TextButton multiPlayer = new TextButton("Multi Player", skin);
 TextButton options = new TextButton("Options", skin);
 TextButton exit = new TextButton("Exit", skin);
 int x = Gdx.graphics.getHeight();
 int unit = x / 6;
 float scale = unit / singlePlayer.getHeight();
 float width = singlePlayer.getWidth() * scale;
 menuTable.add( singlePlayer ).size( width,unit).right().row();
 menuTable.add( multiPlayer ).size( width,unit).right().row();
 menuTable.add( options ).size( width,unit).right().row();
 menuTable.add( exit ).size( width,unit).right().row();
asked May 1, 2015 at 12:33

2 Answers 2

6

Use textButton.getLabel().setFontScale(x, y).

answered May 1, 2015 at 12:54
Sign up to request clarification or add additional context in comments.

4 Comments

It worked but it looks really bad and blurry :/ isn't there anyway to change size instead of scaling?
Size and scaling are effectively the same thing when it gets to the screen. You need to start with a higher resolution font texture. And make sure the texture is loaded with mip-mapping on (and use texture filters MipMapLinearLinear, Linear).
You will need to use a bigger BitmapFont and update the skin JSON file accordingly.
Use a bigger BitmapFont, then assume a high density device. Then work out a formula to scale down the font on lower density devices using Gdx.graphics.getDensity()
2

Use FreeTypeFontGenerator to set a font and then Update the button:

private void UpdateButton(float xf, float width, float height) {
 if (xf > grayButton.getX() + grayButton.getWidth() / 2) {
 width += 20;
 height += 20;
 } else {
 width -= 20;
 height -= 20;
 }
 params.size = (int) ((int) width / 4.0);
 fontB = generator.generateFont(params);
 grayButtonStyle.font = fontB;
 grayButton.remove();
 grayButton = new TextButton("abcABC", grayButtonStyle);
 grayButton.setSize(width, height);
 grayButton.setPosition((screenWidth - width) / 2, (screenHeight - height) / 2);
 grayButton.addListener(new ClickListener() {
 @Override
 public void clicked(InputEvent event, float x, float y) {
 UpdateButton(Gdx.input.getX(), grayButton.getWidth(), grayButton.getHeight());
 }
 });
 stage.addActor(grayButton);
}
Anonymous
87.4k15 gold badges163 silver badges181 bronze badges
answered Jan 24, 2017 at 4:32

1 Comment

Isn't it slow to generate fonts on the go? Also can you explain what does UpdateButton function do? So I can mark your answer as accepted.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.