How get a string width in Libgdx?

后端 未结 3 790
一整个雨季
一整个雨季 2020-12-01 09:06

I wonder know how to get the width of my string in pixels

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 10:06

    If you use skins in UI it is a hassle to find out the correct font to feed into the GlyphLayout.

    In this case I use a throw away instance of Label to figure everything out for me then ask the Label for the width.

    Skin skin = getMySkin();
    Label cellExample = new Label("888.88888", skin);
    cellExample.layout();
    float cellWidth = cellExample.getWidth();
    
    Table table = new Table(skin);
    table.defaults().width(cellWidth);
    // fill table width cells ...
    

    This is not the answer if you want to position the text yourself, but it is useful to make a UI layout stable and less dependent on the actual content of the cells.

提交回复
热议问题