Search code examples
libgdx

how to align text in a textbutton


I have a rectangle button and I am trying to align text in it like so for example:

  but1.getLabel().setAlignment(Align.left);

I know that there are different options such as Align.right, bottomRight etc. However, all these options align the text around the edges of the rectangle like below. In this case bottomLeft and bottomRight:

enter image description here

enter image description here

I am looking for a way to specify something in between. For instance, I would like to align the text to be between bottomLeft and the center (not on the edges? For example:

enter image description here


Solution

  • A TextButton is essentially just a table with a Label component. This means that you can apply padding to it. If you want the label to be offset from the bottom left corner, just give the appropriate padding. This would look something like this:

    TextButton tb = new TextButton("1", skin);
    tb.pad(20, 5, 0, 15);
    

    Of course, you can always add alignments or other paddings if necessary, but this is how you would manipulate the label's position.