我正在尝试在摆动对象(jLabel 等)中显示表情符号,所以为了做到这一点,我正在使用这个库和以下类,以便表情符号仅显示
jLabel.setIcon(new EmojiIcon(":poop:"));
它适用于所有支持表情符号的字体,除了那些有颜色的字体(即EmojiOne和Noto Color Emoji)
单色表情符号有什么意义?
public class EmojiIcon implements Icon {
private Font font;
private final static int DEFAULT_SIZE = 32;
private int width = DEFAULT_SIZE;
private int height = DEFAULT_SIZE;
private String emoji;
public EmojiIcon (String iconString){
this.emoji = EmojiParser.parseToUnicode(iconString);
setFont("emojione-android");
recalculateIconWidth(emoji);
}
public void setFont(String strFont){
try {
font = Font.createFont(Font.TRUETYPE_FONT, new File("fonts/"+strFont+".ttf")).deriveFont(48f);
} catch (FontFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void recalculateIconWidth( String iconString ){
FontRenderContext frc = new FontRenderContext( null, true, true );
Rectangle2D bounds = font.getStringBounds(iconString, frc );
width = (int) bounds.getWidth();
height = (int) bounds.getHeight();
}
@Override
public void paintIcon( Component c, Graphics g, int x, int y ){
Graphics2D g2d = (Graphics2D) g;
g2d.setFont( font );
g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON );
g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
g2d.drawString( emoji, 0, height );
}
}
我在网上寻找字体是否合法,所以我在这里发现它不是标准化的(至少使用 .ttf 字体)。有解决方法吗?我真的很想用EmojiOne 字体