调整文本字体大小以适合Javafx8中的文本框

时间:2019-06-18 15:37:53

标签: java javafx text javafx-8

我有一个包含文本框的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行(有时会溢出到第二行),因此太小了。 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);

0 个答案:

没有答案
相关问题