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();
2 Answers 2
Use textButton.getLabel().setFontScale(x, y).
answered May 1, 2015 at 12:54
Tenfour04
95.1k14 gold badges125 silver badges181 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
Arda Kara
It worked but it looks really bad and blurry :/ isn't there anyway to change size instead of scaling?
Tenfour04
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).
David Saltares
You will need to use a bigger BitmapFont and update the skin JSON file accordingly.
Mofe Ejegi
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()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
1 Comment
Arda Kara
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.
lang-java