本文介绍了在Java中自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Java中修复与自定义字体的问题?
How to fix problem with custom fonts in Java?
例如,我的应用程序使用的字体,那就是并非所有的计算机。我能以某种方式将其包含在编译的可执行文件,然后从那里调用它,如果客户端计算机上不存在呢?
For example, my app uses font, that isn't on all computers. Can I somehow include it in compiled executable and then call it from there, if it doesn't exists on clients computer?
什么其他选择?我可以让所有的字体字符为图像(以前,在某些图形应用程序),然后为每个字符显示的图片是它好吗?
What are other alternatives? I could make all fonts chars as images (before, in some graphics app) and then display image for each char... is it ok?
推荐答案
下面是一个实用的方法,我使用加载从.TTF文件中的字体文件(可捆绑):
Here's an utility method I'm using to load a font file from a .ttf file (can be bundled):
private static final Font SERIF_FONT = new Font("serif", Font.PLAIN, 24);
private static Font getFont(String name) {
Font font = null;
if (name == null) {
return SERIF_FONT;
}
try {
// load from a cache map, if exists
if (fonts != null && (font = fonts.get(name)) != null) {
return font;
}
String fName = Params.get().getFontPath() + name;
File fontFile = new File(fName);
font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
ge.registerFont(font);
fonts.put(name, font);
} catch (Exception ex) {
log.info(name + " not loaded. Using serif font.");
font = SERIF_FONT;
}
return font;
}
这篇关于在Java中自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!