java - Center Justify Text in libgdx -
i have started libgdx , have figured out how center text it. having trouble center justifying text. wondering if can help. have attach code centering. thank 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, 1 cause left me\n" + "two, 2 family\n" + "three, 3 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 , set targetwidth screen width, aligh.center, , set wrap true. also, set x = 0 text centered across whole screen.
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, 1 cause left me\n" + "two, 2 family\n" + "three, 3 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(); }
Comments
Post a Comment