【问题标题】:SWT Load Font in a RCP AppSWT 在 RCP 应用程序中加载字体
【发布时间】:2013-11-27 17:05:35
【问题描述】:

这是我想做的:

Display.getCurrent().loadFont("fonts/helveticaNeueBold_iOS7.ttf")
  • 在测试器中工作即具有入口点的类)。
  • 在 RCP 应用中工作。

加载机制有何不同?我应该获取 ttf 文件,然后提取它的路径吗?

【问题讨论】:

    标签: java fonts swt eclipse-rcp


    【解决方案1】:

    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
    • 您尝试在 loadFont 方法中使用什么路径?
    • 请这样做:Display.getCurrent().loadFont(file.toString());
    【解决方案2】:

    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 的访问受限。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      • 1970-01-01
      • 2020-08-21
      • 2013-09-14
      • 1970-01-01
      相关资源
      最近更新 更多