JavaFX字体派生粗体 [英] Javafx font derive bold
问题描述
我想为我的javafx应用程序动态更改(加载的)文本字体,所以我执行了以下代码:
I wanted to change a (loaded) text font dinamically for my javafx application, so I did this code:
Font font = Font.loadFont(Fonts.class.getClassLoader().getResource("path/font.woff").toExternalForm(), 25);
Font bold = Font.font(font.getFamily(), FontWeight.BOLD, font.getSize());
Font italic = Font.font(font.getFamily(), FontPosture.ITALIC, font.getSize());
Font boldItalic = Font.font(font.getFamily(), FontWeight.BOLD, FontPosture.ITALIC, font.getSize());
但是当我尝试将文本字体更改为粗体或斜体时,没有任何变化,并且我似乎没有找到像java.awt.Font#deriveFont中那样的方法,您可以从另一种字体派生斜体或粗体.
应用字体的代码:
but when I try to change the text font to bold or italic nothing changes, and I don't seem to locate some method like the one in java.awt.Font#deriveFont where you could derive italic or bold from another font.
Code to apply the font:
public void applyFont(javafx.scene.text.Text text) {
text.setFont(font);
}
更多日志:
我放了一些日志,说他得到了什么字体,并打印了Font#toString()结果:
More log:
I put some log that says what font is he getting and prints the Font#toString() result:
getFont(bold=false, italic=false) -> Font[name=<Name> Regular, family=<Name>, style=Regular, size=25.0]
getFont(bold=true, italic=false) -> Font[name=<Name> Regular, family=<Name>, style=Regular, size=25.0]
getFont(bold=false, italic=true) -> Font[name=<Name> Regular, family=<Name>, style=Regular, size=25.0]
getFont(bold=true, italic=true) -> Font[name=<Name> Regular, family=<Name>, style=Regular, size=25.0]
但是如果我将字体加载中的第一行代码替换为
But if I replace the first line of code in the font loading with
font = Font.font(null, 25);//get the system's default font
它有效,我得到以下输出:
It works and I get this output:
getFont(bold=false, italic=false) -> Font[name=System Regular, family=System, style=Regular, size=25.0]
getFont(bold=true, italic=false) -> Font[name=System Bold, family=System, style=Bold, size=25.0]
getFont(bold=false, italic=true) -> Font[name=System Italic, family=System, style=Italic, size=25.0]
getFont(bold=true, italic=true) -> Font[name=System Bold Italic, family=System, style=Bold Italic, size=25.0]
推荐答案
您需要加载另一个字体文件,该文件将提供适当的姿势或权重.
You need to load another font file, that will provide appropriate Posture or Weight.
对于每种流行字体(包括系统,当您调用Font.font(null, 25)
时,都会使用该字体),系统中都有几个字体文件:一个用于常规,一个用于斜体,一个用于粗体等.
For every popular font (including System, that is used, when you call Font.font(null, 25)
) there are several font files in the system: one for regular, one for italic, one for bold etc.
例如,对于Times New Roman,我的字体文件夹中有四个文件:
For instance, for Times New Roman there are four files in my Fonts folder:
- Times New Roman Bold Italic.ttf
- Times New Roman Bold.ttf
- Times New Roman Italic.ttf
- Times New Roman.ttf
这篇关于JavaFX字体派生粗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!