【问题标题】:Some fonts won't load while others do, using JavaFX 8 and CSS使用 JavaFX 8 和 CSS,某些字体不会加载,而其他字体则不会加载
【发布时间】:2017-05-16 09:06:24
【问题描述】:

我正在尝试使用 Apple's San Francisco font 为我的 Java 应用程序设置皮肤。我已经尝试了我找到的所有方法,但仍然无法加载 SF 字体,而其他自定义字体似乎可以工作。

以下是我的 CSS 代码,如果我使用来自 this answer 的 TRON 字体,但不能使用 SF 字体,它可以工作。 ID 为title 的元素是一个Label 对象,其中文本在构造函数中设置,除了设置ID 之外,不执行其他操作。 From here,我知道这应该可行。知道为什么没有吗?

(请注意,只要适用,我还尝试使用“San Francisco”、“SF、“SF UI”和“SF UI Text”来代替“SF UI Text Regular”。)

@font-face {
    src: url("SFText-Regular.otf");
}

#title .text {
    -fx-font-family: "SF UI Text Regular";
}

我正在使用 Java 1.8.0_121-b13 和 JavaFX 8.0.121-b13。

我尝试过的其他方法

旧的 CSS 方法,无济于事:

@font-face {
    font-family: "SF";
    src: url("SFText-Regular.otf");
}

#title .text {
    -fx-font-family: "SF";
}

在线加载和设置字体 (from here):

title.setFont(Font.loadFont("file:SFText-Regular.otf", 10));
title.setStyle("-fx-font-family: 'SF UI Text Regular';");

最后单独加载字体(from here):

Font.loadFont(ControlPane.class.getResource("SFText-Regular.otf").toExternalForm(), 10);
        title2.setStyle("-fx-font-family: 'San Francisco';");

以上所有内容都适用于 TRON 字体,但不适用于 SF 字体。这个问题是否可能源于他们的设计或类似的东西?

【问题讨论】:

    标签: css fonts javafx-8


    【解决方案1】:

    这对我有用:

    1. 下载的字体来自:

      https://www.fontify.me/cm/28ea2e32b70da0a2480848ba03cd6146/SF-UI-Text-Regular.ttf

      这只是一个来自互联网的随机链接,我不知道它是否会保持良好。

    2. 将下载的字体与下面的示例代码放在同一目录中。
    3. 在 OS X 中右键单击文件并选择“获取信息”以查找字体名称。

      在这种情况下是:SF UI Text Regular

    4. 运行示例代码。

    使用 SF UI Text Regular 字体:

    使用我的 OS X 10.9.5 框上的默认字体(注释掉代码中的 setStyle 行):

    与提供的低质量屏幕截图略有不同,但在我的 PC 上并排运行两者时更明显。

    我使用的字体是.ttf 字体而不是.otf 字体。我不知道这是否会有所不同。

    很遗憾,我无法充分解释为什么苹果会从树上掉下来。

    san-fran.css

    @font-face {
        src: url("SF-UI-Text-Regular.ttf");
    }
    

    SanFranFont.java

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    public class SanFranFont extends Application {    
        @Override
        public void start(final Stage stage) throws Exception {
            Label text = new Label("After dinner, the weather being warm, we went into the garden and drank thea, under the shade of some apple trees...he told me, he was just in the same situation, as when formerly, the notion of gravitation came into his mind. It was occasion'd by the fall of an apple, as he sat in contemplative mood. Why should that apple always descend perpendicularly to the ground, thought he to himself...");
            text.setWrapText(true);
            text.setPrefWidth(400);
    
            final Scene layout = new Scene(new StackPane(text));
            layout.getStylesheets().add(
                    getClass().getResource("san-fran.css").toExternalForm()
            );
    
            text.setStyle("-fx-font-family: \"SF UI Text Regular\";");
    
            stage.setScene(layout);
            stage.show();
        }
    
        public static void main(String[] args) throws Exception {
            launch(args);
        }
    }
    

    【讨论】:

    • 谢谢。我在使用 Open Sans TTF 字体时遇到了同样的问题,因此 .otf 格式可能不是问题(或者至少是唯一的问题)。
    猜你喜欢
    • 2015-07-04
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    相关资源
    最近更新 更多