How do I display emoji in JavaFX on OS X?

半腔热情 提交于 2019-12-01 17:35:25

I think that the default Apple Emoji Font (Apple Color Emoji) can't be rendered in JavaFX. Normally a font defines a vector based visualization for each character. There are some fonts that use images for specific characters. The emojis in the Apple font are defined as images. You can easily test it by adding an emoji in Pages or Keynote and change the font size to 500 or so. By doing so you will see that the emoji is pixelated. I think Apple added each emoji in several dimensions to the font (like 32px, 64px, 128px, 256px) but if you make it big enough you can see the pixels.

I think that JavaFX can't render that kind of font and supports only vector based fonts. Therefore the emoji isn't rendered on your Mac.

What you can do: You can add a vector based emoji font to your project (in the resources) and load it in Java code or by using CSS (see http://www.guigarage.com/2014/10/integrate-custom-fonts-javafx-application-using-css/). If you did it by using CSS you can simply define a CSS rule for the ".text" class and define the loaded font. By doing so you will set the font for (mostly) all texts in your application. There are some cases where you need some additional CSS rules to have really everything shown in your custom font.

If you want to display the emojis only at some special points you can check if using a JavaFX TextFlow isn't a better approach (see http://www.guigarage.com/2013/09/make-your-app-smile-d/). Here you can parse a text and split it in several JavaFX text nodes that are part of a TextFlow. In that case you will end in some text nodes that contain an emoji and you can change the font only for this text nodes. By doing so the rest of your application can be displayed in the default font or any font of your choose.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!