问题
Here's what I want to do:
Display.getCurrent().loadFont("fonts/helveticaNeueBold_iOS7.ttf")
- Works in a tester (i.e. class with entry point).
- Doesn't work in an RCP app.
How do the loading mechanisms differ? Should I get the ttf
file, then extract the path to it?
回答1:
Eclipse bundles have different paths (something like "bundleentry://bundle_number/path_to_your_file"). You might want to use FileLocator
to load files properly. For example:
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());
Also, please check other methods, available within FileLocator
.
回答2:
Alexander's answer could also be helpful to some.
For me, the following snippet did the trick:
final String path = "fonts/helveticaNeueBold_iOS7.ttf";
final URL pathUrl = BundleUtility.find(PLUGIN_ID, path);
final boolean isFontLoaded = Display.getCurrent().loadFont(pathUrl.toExternalForm());
Although beware that BundleUtility
has restricted access.
来源:https://stackoverflow.com/questions/19981868/swt-load-font-in-a-rcp-app