我正在尝试使用 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 字体。这个问题是否可能源于他们的设计或类似的东西?
最佳答案
这对我有用:
从以下位置下载的字体:
https://www.fontify.me/cm/28ea2e32b70da0a2480848ba03cd6146/SF-UI-Text-Regular.ttf
That is just a random link from the internet, I don't know if it will stay good.
- 将下载的字体放在与下面示例代码相同的目录中。
- 在 OS X 中右键单击该文件并选择“获取信息”以查找字体名称。
In this case it is:
SF UI Text Regular
. - 运行示例代码。
使用 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);
}
}
关于css - 使用 JavaFX 8 和 CSS,某些字体不会加载,而其他字体会加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43996987/