简体   繁体   中英

How to render an emoji with libgdx

I have loaded a font with emoji support and I am trying to render a string with emoji's with libgdx. However, it renders the text, but not the emojis.

Code

Load Font

FileHandle fontFile = Gdx.files.internal("path/to/file.ttf");
FreeTypeFontGenerator g = new FreeTypeFontGenerator(fontFile);
FreeTypeFontGenerator.FreeTypeFontParameter p = new FreeTypeFontGenerator.FreeTypeFontParameter();
// Some config here with p
BitmapFont emojiFont= g.generateFont(p);

Render Font

public static void renderFont(SpriteBatch sb, BitmapFont font, String msg, float x, float y, Color c) {
  font.setColor(c);
  font.draw(sb, msg, x, y);
}

String str = "emoji ❤ \uD83D\uDC49 test \uD83D\uDC49 \uD83D\uDC4D test \uD83D\uDE03"
renderFont(sb, emojiFont, str, x, y, new Color(-597249));

Output

emoji test test

Questions

  1. Why is libgdx not rendering the emojis?
  2. What do I need to change to render the emojis?

FreeTypeFontGenerator creates a BitmapFont from your TTF file. Most likely your created font does not contain the emojis.

You don't show the interesting part of your code: the parameters you set. Add the emojis you want to use to the parameters:

p.characters = "characters you want to use";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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