As of right now, there really isn't a way to render Right to Left text, as shown by this thread about it. So the only way to really do it is to reverse the text with StringBuilder
, and then display that. A more efficient way to render the reversed text is to create a method that will display the text accordingly, so you don't have to reverse it every time you try to write Right to Left text. If you create a method, you will be able to implement the RTL text into chats, names, or other graphics that require RTL fonts.
I also recommend converting your Bitmap to a .ttf file so that it is easy to use your custom font while also keeping a good quality. You can then use the FreeTypeFontGenerator
to render your font nicely. If you cannot convert your Bitmap
to a font you could also use your method of displaying text in the below method. A really good alternative is the Hiero library. You can select the RTL text check box.
Here is an example of a method that you could create to render the text (using the FreeTypeFontGenerator
):
// Keep the generator here so that it is not created each time
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fontFile.ttf"));
public void renderRTL(float x, float y, int fontSize, String text) {
batch.begin(); // Lets you draw on the screen
// Reverses the text given
StringBuilder builder = new StringBuilder();
builder.append(text);
builder.reverse();
String outputText = builder.toString();
// Creates the font
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.size = fontSize;
parameter.characters = "ALL HEBREW CHARACTERS"; // Put all your Hebrew characters in this String
scoreFont = generator.generateFont(parameter);
scoreFont.setColor(Color.BLACK);
scoreFont.draw(batch, outputText, x, y); // Actually draws the text
generator.dispose(); // Clears memory
batch.end();
}
Make sure to add all of these dependencies into your build.gradle
file (in the dependencies section):
compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
You could optionally add a color parameter (in RGBA format) to your method for more functionality. Hope it helps!