如何在JavaFX中设置字体的字母间距(也称为跟踪)? [英] How to set the letter spacing (aka tracking) of a Font in JavaFX?
问题描述
互联网告诉我它可以在JavaFX 1.x中使用(我发现的大多数示例都是.fx格式,现在已经过时了).但是现在(JavaFX 2. *)似乎无法执行.
我可以想到一种实现所需效果的方法:将每个字母填充到Text
中,然后将所有字母添加到FlowPane
中,并且将HGap
设置为所需的间距.但是,我宁愿将此作为最后的手段.
哦,我也尝试过将-fx-letter-spacing
放到样式表中,也没用.
The internet teaches me it was available in JavaFX 1.x (most examples I found, were in the .fx format, which is now obsolete). But it now (JavaFX 2.*) seems impossible to do.
I can think of one way for accomplishing the effect I want: stuff every letter in a Text
and add all of them to a FlowPane
with the HGap
set to the wanted spacing. But I'd rather use this as a last resort.
Oh, I've also tried putting -fx-letter-spacing
in my stylesheet, didn't work either.
推荐答案
我制作了一个适合我需要的自定义类(警告:功能不多)
I made a custom class that fits my needs (warning: doesn't have a lot of functionality)
public class LetterSpacedText extends FlowPane {
private Font font;
private Color fill;
public LetterSpacedText(String s, double spacing) {
setText(s);
setHgap(spacing);
}
public void setText(String s) {
getChildren().clear();
for (int i = 0; i < s.length(); i++) {
getChildren().add(new Text("" + s.charAt(i)));
}
setFont(this.font);
setFill(this.fill);
}
public void setFont(Font font) {
if (font != null) {
this.font = font;
for (Node t : getChildren()) {
((Text) t).setFont(font);
}
}
}
public void setFill(Color fill) {
if(fill != null) {
this.fill = fill;
for (Node t : getChildren()) {
((Text) t).setFill(fill);
}
}
}
}
这篇关于如何在JavaFX中设置字体的字母间距(也称为跟踪)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!