【发布时间】:2013-11-27 17:05:35
【问题描述】:
这是我想做的:
Display.getCurrent().loadFont("fonts/helveticaNeueBold_iOS7.ttf")
- 在测试器中工作(即具有入口点的类)。
- 不在 RCP 应用中工作。
加载机制有何不同?我应该获取 ttf 文件,然后提取它的路径吗?
【问题讨论】:
标签: java fonts swt eclipse-rcp
这是我想做的:
Display.getCurrent().loadFont("fonts/helveticaNeueBold_iOS7.ttf")
加载机制有何不同?我应该获取 ttf 文件,然后提取它的路径吗?
【问题讨论】:
标签: java fonts swt eclipse-rcp
Eclipse 包有不同的路径(类似于“bundleentry://bundle_number/path_to_your_file”)。您可能希望使用FileLocator 正确加载文件。例如:
Bundle bundle = Activator.getDefault().getBundle();
Path path = new Path("fonts/helveticaNeueBold_iOS7.ttf");
URL url = FileLocator.find(bundle, path, Collections.EMPTY_MAP);
URL fileUrl = null;
try {
fileUrl = FileLocator.toFileURL(url);
}
catch (IOException e) {
// Will happen if the file cannot be read for some reason
e.printStackTrace();
}
File file = new File(fileUrl.getPath());
boolean loadFont = Display.getCurrent().loadFont(file.toString());
另外,请查看FileLocator 中提供的其他方法。
【讨论】:
loadFont API 返回 FALSE。
Alexander's answer 也可能对某些人有所帮助。
对我来说,以下 sn-p 成功了:
final String path = "fonts/helveticaNeueBold_iOS7.ttf";
final URL pathUrl = BundleUtility.find(PLUGIN_ID, path);
final boolean isFontLoaded = Display.getCurrent().loadFont(pathUrl.toExternalForm());
虽然请注意BundleUtility 的访问受限。
【讨论】: