简体   繁体   English

如何更改自定义字体的文字大小?

[英]How to change custom font text size?

I've uploaded my own font in eclipse like this: 我在Eclipse中上传了自己的字体,如下所示:

Font buttonFontDefault = Font.loadFont(MenuView.class.getClassLoader().getResourceAsStream("res/font/niagra_eng.TTF"), 100);

And it works just fine. 而且效果很好。 The only problem I have, is that the only way of changing the text size, of buttons and labels written with this font, is to make a new font where the only difference is the size in the end of the code. 我唯一的问题是,更改用此字体编写的按钮和标签的文本大小的唯一方法是制作一种新字体,唯一的区别是代码末尾的大小。 I also tried the following: 我还尝试了以下方法:

button.setScaleX(0.50);
button.setScaleY(0.50);

The code above works, but then it ruins my GridPane layout, because the gridpane acts according to the original button size. 上面的代码可以正常工作,但是随后它破坏了我的GridPane布局,因为网格窗格根据原始按钮的大小起作用。 I'd rather have a solution without any use of css, but if that's not a possibility then I guess I have no other choice. 我宁愿有一个不使用CSS的解决方案,但是如果那不可能,那么我想我别无选择。 But why shouldn't there be a solution without css? 但是为什么没有CSS的情况下不应该有解决方案呢?

I haven't been able to find any help in the internet so far. 到目前为止,我还没有在互联网上找到任何帮助。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

If you want to handle the fonts programmatically you can do it like @ItachiUchiha suggested by defining the font size when loading the font: 如果要以编程方式处理字体,则可以像@ItachiUchiha那样通过在加载字体时定义字体大小来实现:

Font.loadFont(String name, double size)

The drawback of this method - as well as handling it programmatically at all - is that for each font size you will require a font instance handled by your code. 这种方法的缺点-以及以编程方式进行处理-的缺点是,对于每种字体大小,您都需要一个由代码处理的字体实例。

A cleaner solution in my opinion is to handle the fonts through CSS. 我认为一个更清洁的解决方案是通过CSS处理字体。 Starting with Java 8, the font families can be defined through CSS: 从Java 8开始,可以通过CSS定义字体系列:

@font-face {
    font-family: 'Roboto';
    src: url('Roboto-Medium.ttf');
}

.text {
    -fx-font-family: "Roboto";
    -fx-font-size: 18px;
}

For a detailed example, check out this post 有关详细示例,请查看此帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM