简体   繁体   English

在libgdx中居中对齐文本

[英]Center Justify Text in libgdx

I have just started with LibGdx and I have figured out how to center text with it. 我刚开始使用LibGdx,我已经想出了如何使用它来居中文本。 Now I am having trouble with center justifying text. 现在我遇到了中心对齐文本的问题。 I was wondering if someone can help. 我想知道是否有人可以提供帮助。 I have attach my code for centering. 我附上了我的代码以进行居中。 Thank you in advance. 先感谢您。

package com.tutorials.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class TextDemo extends ApplicationAdapter {
    SpriteBatch batch;
    BitmapFont font;
    String myText;
    GlyphLayout layout = new GlyphLayout();

    @Override
    public void create () {
        batch = new SpriteBatch();
        font = new BitmapFont(Gdx.files.internal("myFont.fnt"));
        myText = "I took one, one cause you left me\n"
               + "Two, two for my family\n"
               + "Three, three for my heartache";
        layout.setText(font,myText);
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        float x = Gdx.graphics.getWidth()/2 - layout.width/2;
        float y = Gdx.graphics.getHeight()/2 + layout.height/2;

        batch.begin();
        font.draw(batch,layout,x,y);//Center Text
        batch.end();
}

You can use following setText() method instead and set targetWidth to screen width, Aligh.center, and set wrap to true. 您可以使用以下setText()方法,并将targetWidth设置为屏幕宽度,Aligh.center,并将wrap设置为true。 Also, set x = 0 so the text is centered across the whole screen. 此外,设置x = 0,使文本在整个屏幕中居中。

import com.badlogic.gdx.graphics.g2d.GlyphLayout;

public void setText(BitmapFont font,
                java.lang.CharSequence str,
                Color color,
                float targetWidth,
                int halign,
                boolean wrap)

Updated example: 更新示例:

@Override
public void create () {
    batch = new SpriteBatch();
    font = new BitmapFont(Gdx.files.internal("myFont.fnt"));
    myText = "I took one, one cause you left me\n"
           + "Two, two for my family\n"
           + "Three, three for my heartache";
    layout.setText(font,myText,Color.BLACK,Gdx.graphics.getWidth(),Align.center,true);
}

@Override
public void render () {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    float x = 0;
    float y = Gdx.graphics.getHeight()/2 + layout.height/2;

    batch.begin();
    font.draw(batch,layout,x,y);//Center Text
    batch.end();
}

Instead of using the font.draw method, use the following one instead... 而不是使用font.draw方法,而是使用以下方法...

public TextBounds drawMultiLine (Batch batch, CharSequence str, float x, float y, float alignmentWidth, HAlignment alignment)

alignmentWidth is the max width you want your text to take up. alignmentWidth是您希望文本占用的最大宽度。 Any more than that and it will wrap. 不止于此,它将包装。 Set it to something stupidly high if you don't want wrapping. 如果你不想要包装,将它设置为愚蠢的高度。

'alignment' is the key thing and takes an HAlignment enum and can be either LEFT , RIGHT or CENTER 'alignment'是关键,它采用HAlignment枚举,可以是LEFTRIGHTCENTER

The batch , str , x and y parameters are the same as you're already doing. batchstrxy参数与您正在执行的相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM