在 JavaFX CSS 中指定外部字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12173288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 20:01:43  来源:igfitidea点击:

Specifying external font in JavaFX CSS

cssfontsjavafx

提问by syscreat

Is it possible to specify a font using CSS in JavaFX application? I have an FXML scene and the corresponding CSS file. In Java code, it's possible to specify a font using the setFontmethod:

是否可以在 JavaFX 应用程序中使用 CSS 指定字体?我有一个 FXML 场景和相应的 CSS 文件。在 Java 代码中,可以使用以下setFont方法指定字体:

text.setFont(Font.loadFont("file:resources/fonts/SourceSansPro-Bold.ttf", 12));

I tried different constructions, like:

我尝试了不同的结构,例如:

-fx-font-family: url("../fonts/SourceSansPro-Bold.ttf");

But it doesn't work.

但它不起作用。

采纳答案by Andrea Turri

You were close to the solution:

您已接近解决方案:

-fx-font-family: 'Helvetica', Arial, sans-serif;

Try this, it should work fine.

试试这个,它应该可以正常工作。



Maybe this could help? (I'm not an expert in JavaFX)

也许这会有所帮助?(我不是 JavaFX 专家)

https://forums.oracle.com/forums/thread.jspa?messageID=10107625

https://forums.oracle.com/forums/thread.jspa?messageID=10107625



EDIT:

编辑

Load your font:

加载你的字体:

@font-face {
    font-family: Delicious;
    src: url('Delicious-Roman.otf');
}

Use your font with -fx-:

使用您的字体-fx-

-fx-font-family: 'Delicious';

回答by jewelsea

I use a combination of application code and CSS to style via an external font.

我使用应用程序代码和 CSS 的组合通过外部字体设置样式。

I place the loadFontcall inside an overridden Application init method to make sure it is invoked before anything much happened in the application.

我将loadFont调用放在一个重写的 Application init 方法中,以确保在应用程序中发生任何事情之前调用它。

Font.loadFont(CustomFontTest.class.getResource("TRON.TTF").toExternalForm(), 10);

To use the font, I reference the font by font family in CSS:

为了使用字体,我在 CSS 中通过字体系列引用字体:

.menu-bar {
    -fx-background-color: transparent;
    -fx-font-family: TRON;
    -fx-font-size: 40px;
}

.context-menu {
    -fx-font-family: TRON;
    -fx-background-color: transparent;
    -fx-font-size: 12px;
}

Nice that the CSS sizes the fonts fine. Even when the font is loaded at size 10, the font was resized correctly to what is specified in the CSS -fx-font-sizespecifications.

很高兴 CSS 可以很好地调整字体的大小。即使以 10 号加载字体,字体也已正确调整为 CSS-fx-font-size规范中指定的大小。

Inline styling of a label via CSS using a Fontloaded during application initialization also works fine:

使用Font在应用程序初始化期间加载的CSS 通过 CSS 内联样式标签也可以正常工作:

Label testControl = new Label("TRON");
testControl.setStyle("-fx-font-family: TRON; -fx-font-size: 120;");

The TRON fontwas downloaded from dafont and placed in the same directory as the CustomFontTest class and copied to the build output directory by the build system.

所述TRON字体从dafont下载并通过构建系统放置在相同的目录中CustomFontTest类和复制到生成输出目录。

Answer copied from my answer to a forum post on "Using Custom Fonts".

从我对“使用自定义字体”论坛帖子的回答中复制的答案。

回答by rli

Just found out one more detail: In JavaFX-8 if you want to have regular and bold variants of the same font you can specify them with two instances of @font-face. Then you can use -fx-font-weight: bold;

刚刚发现了一个细节:在 JavaFX-8 中,如果您想拥有相同字体的常规和粗体变体,您可以使用 @font-face 的两个实例来指定它们。然后你可以使用 -fx-font-weight: bold;

@font-face {
    font-family: 'Droid Sans';
    src: url('DroidSans.ttf');
}

@font-face {
    font-family: 'Droid Sans Bold';
    src: url('DroidSans-Bold.ttf');
}

.root {
    -fx-font-family: 'Droid Sans'; 
}

.table-row-cell:focused .text {
    -fx-font-weight: bold;
}

回答by RichardK

Another way is to load font using FileIUnputStream, thus you don't need to use css:

另一种方法是使用 FileIUnputStream 加载字体,因此您不需要使用 css:

 @FXML
    private Label myLabel;

@Override
    public void initialize(URL arg0, ResourceBundle arg1){

Font myFont = null;

        try {
            myFont = Font.loadFont(new FileInputStream(new File("patch_to_font.ttf")), 10);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        myLabel.setFont(myFont);

}

回答by FxMax

JavaFx CSS fonts (import .ttf):

JavaFx CSS 字体(导入 .ttf):

/* import fonts */
@font-face {
    font-family: "Open Sans";
    font-style: normal;
    font-weight: 400;
    src: url("/fonts/OpenSans-Regular.ttf");
}
@font-face {
    font-family: "Open Sans Light";
    font-style: normal;
    font-weight: 300;
    src: url("/fonts/OpenSans-Light.ttf");
}
@font-face {
    font-family: "Open Sans Bold";
    font-style: normal;
    font-weight: 700;
    src: url("/fonts/OpenSans-Bold.ttf");
}
@font-face {
    font-family: "Open Sans ExtraBold";
    font-style: normal;
    font-weight: 900;
    src: url("/fonts/OpenSans-ExtraBold.ttf");
}

/* Set fonts */

.settings-name{
    -fx-font-size: 33px;
    -fx-font-family: "Open Sans Light";
    -fx-font-weight: 300;
    -fx-text-fill: #09f;
}

.settings-username{
    -fx-font-size: 19px;
    -fx-font-family: "Open Sans ExtraBold";
    -fx-font-weight: 900;
    -fx-text-fill: #09f;
}