首页 > 解决方案 > 调整文本字体大小以适应 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);

标签: javajavafxtextjavafx-8

解决方案


推荐阅读