java - 调整文本字体大小以适应 Javafx8 中的文本框
问题描述
我有一个包含文本框的 javafx gui。我想知道如何为要设置的文本找到最佳字体大小。
无法调整文本框的大小以适合文本,并且无法截断/溢出文本。
在程序中,文本框可以改变它的尺寸,文本可以改变字体(例如,从 Arial 到 Comic Sans),或者改变文本本身(从单个词到段落),我需要确保文本调整大小以适合文本框。
文本框可以是任何包含文本的节点,例如 label、textFlow、textArea...
我写了 3 次不同的尝试来尝试开发这种算法,但都没有奏效。
主要代码:
public class Main extends Application{
String text = new String("Lorem ipsum dolor sit amet, consectetur adipiscing elit. In scelerisque elit ante, eu lacinia dui molestie quis. Donec consectetur elementum.");
TextFlow textBox;
AnchorPane anchorPane;
public static void main(String[] args) {
launch();
}
@Override
public void start(Stage primaryStage) throws Exception{
textBox = new TextFlow(new Text(text));
textBox.setTextAlignment(TextAlignment.CENTER);
anchorPane = new AnchorPane(textBox);
AnchorPane.setTopAnchor(textBox,0d);
AnchorPane.setBottomAnchor(textBox,0d);
AnchorPane.setRightAnchor(textBox,0d);
AnchorPane.setLeftAnchor(textBox,0d);
Scene scene = new Scene(anchorPane);
primaryStage.setScene(scene);
primaryStage.show();
textBox.widthProperty().addListener(new ChangeListener<Number>(){
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue){
autoSizeTextBoxText();
}
});
textBox.heightProperty().addListener(new ChangeListener<Number>(){
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue){
autoSizeTextBoxText();
}
});
}
public void setTextBoxFont(Font font){
((Text)textBox.getChildren().get(0)).setFont(font);
autoSizeTextBoxText();
}
public void setTextBoxText(String text){
((Text)textBox.getChildren().get(0)).setText(text);
autoSizeTextBoxText();
}
public void autoSizeTextBoxText(){
Font font = TextAndFontHelper.getOptimalFont(((Text)textBox.getChildren().get(0)).getText(),textBox,((Text)textBox.getChildren().get(0)).getFont());
System.out.println(font.getSize());
((Text)textBox.getChildren().get(0)).setFont(font);
anchorPane.layout();
textBox.layout();
}
}
public class TextAndFontHelper{
public static double getTextWidth(Text text){
double width = text.getBoundsInLocal().getWidth();
return width;
}
public static double getTextHeight(Text text){
double height = text.getBoundsInLocal().getHeight();
return height;
}
public static double getTextArea(Text text){
double area = getTextHeight(text)*getTextWidth(text);
return area;
}
public static Font getOptimalFont(String textString, Region textBox,Font font){
//... This is the method that I want to write
}
}
这些是我的尝试:
尝试#1:这个尝试非常有问题,每次算法触发时,文本大小都会不断地从一个合适的大小(虽然不完美,因为它可以增加一点)变成一个不适合屏幕的大大小。 https://imgur.com/a/jMDoTeR
Text text = new Text(textString);
if (font != null)
text.setFont(font);
double textBoxArea = textBox.getHeight()*textBox.getWidth();
double textArea = getTextArea(text);
double ratio = textBoxArea/textArea;
return new Font(text.getFont().getFamily(),text.getFont().getSize()*ratio);
尝试#2:这个尝试非常有问题,每次算法触发时,文本大小都会不断地从屏幕过小到过大。https://imgur.com/a/i5IvNTw
Text text = new Text(textString);
if (font != null)
text.setFont(font);
double heightRatio = textBox.getHeight()/getTextHeight(text);
double widthRatio = textBox.getWidth()/getTextWidth(text);
double ratio = heightRatio*widthRatio;
return new Font(text.getFont().getFamily(),text.getFont().getSize()*ratio);
尝试#3:此尝试是基于以下问题进行的:Resize a SKLabelNode font size to fit? . 虽然这个算法没有故障,但文本的大小被调整为仅适合 1 行(有时会溢出到第 2 行),因此太小了。 https://imgur.com/a/LWIH4Zr
Text text = new Text(textString);
if (font != null)
text.setFont(font);
double heightRatio = textBox.getHeight()/getTextHeight(text);
double widthRatio = textBox.getWidth()/getTextWidth(text);
double ratio = Math.min(heightRatio,widthRatio);
return new Font(text.getFont().getFamily(),text.getFont().getSize()*ratio);
解决方案
推荐阅读
- postgresql - Postgres db 的配置步骤
- visual-studio-2010 - 多个 msvcr100.dll 版本 - 差异在哪里?
- java - Java 编码审查词
- java - 从每两个整数创建一个点
- c# - WindowsForm 中具有 UnitOfWork 和 Repository 模式的 DI 使用 Autofac
- c# - 自定义无法运行的地方
- python - spacy数组枚举上的标量变量的索引无效
- android - 透明背景活动中的单选按钮颜色更改
- javascript - javascript - 使用 html 标签循环显示数据
- angular - 如何使用片段在谷歌地图上的位置之间获取多条路径