如果溢出,请调整JavaFX标签的大小 [英] Resize JavaFX Label if overrun

查看:589
本文介绍了如果溢出,请调整JavaFX标签的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在TitledPane的GridPane中有一个Label.我希望它按比例缩小0.05em(如果超出),以免出现三个点("Long Labe ...")->"Long Label"变小.

Label的isOverrun()方法很棒,但是JavaFX不提供这种方法,而且生活也不是希望.
到目前为止,我的工作环境是:

I have a Label in a GridPane in a TitledPane. I want it to downsize stepwise by 0.05em if it is overrun so the three dots ("Long Labe...") dont show up -> "Long Label" in small.

An isOverrun()-method for the Label would be great but JavaFX doesnt supply that and life isn't a wishconcert.
So my workarround so far:

    Bounds tpBounds = tPane.getBoundsInLocal();
    Bounds lblBounds = label.getBoundsInLocal();
    Double fontSize = 1.0;

    while (tpBounds.getWidth() < lblBounds.getWidth() && fontSize > 0.5) {
        fontSize = fontSize-0.05;
        label.setStyle("-fx-font-size: "+fontSize+"em;");

        System.out.println(fontSize+" "+tpBounds.getWidth()+" "+lblBounds.getWidth());
    }

问题:在while循环中,bounds.getWidth()始终显示原始宽度.具有新字体大小的新"宽度刷新速度不够快,无法被while条件捕获,因此字体大小越来越低.
是否有解决方案?


编辑
我通常会问:将Label尺寸缩小到适合大小而不会被截断,真的难吗?!

The problem: during the while loop, bounds.getWidth() is always showing the original width. The "new" width with the new fontsize is not refreshing quick enough to get catched by the while-condition, so the fontsize is getting lower and lower.
Any Solutions?

edit
I ask more commonly: Is it really THAT HARD to make a Label downsize itself, till it fits without truncating?!

推荐答案

这是受内部api启发的黑客.

JavaFX使用com.sun.javafx.scene.control.skin.Utils类进行各种基于文本的计算.这还包括计算溢出文本以将原始文本剪裁多少以及在何处显示省略号文本等.

The JavaFX uses com.sun.javafx.scene.control.skin.Utils class to various text based calculations. This also includes calculation of overrun texts to how much clip the original text and at where to show ellipsis text etc.

对于未包装且不是多行标签的文本,此类仅使用3种方法:

For the not wrapped and not multiline label texts there is only 3 methods used in this class:

static String computeClippedText(Font font, String text, double width, OverrunStyle type, String ellipsisString)
static double computeTextWidth(Font font, String text, double wrappingWidth) {...}
static int computeTruncationIndex(Font font, String text, double width) {...}

由于该类是内部api,因此我仅将这3个方法(以及必要的类变量)复制到了自己的Utils类中,并用作:

Since this class is an internal api, I just copied those 3 methods (along with necessary class variables) to my own Utils class and used as:

@Override
public void start( Stage primaryStage )
{
    final Label label = new Label( "Lorem Ipsum is simply dummy long text of the printing and typesetting industry" );
    label.setFont( Font.font( 10 ) );
    System.out.println( "originalText = " + label.getText() );

    Platform.runLater( () -> 
        {
            Double fontSize = label.getFont().getSize();
            String clippedText = Utils.computeClippedText( label.getFont(), label.getText(), label.getWidth(), label.getTextOverrun(), label.getEllipsisString() );
            Font newFont = label.getFont();

            while ( !label.getText().equals( clippedText ) && fontSize > 0.5 )
            {
                System.out.println( "fontSize = " + fontSize + ", clippedText = " + clippedText );
                fontSize = fontSize - 0.05;
                newFont = Font.font( label.getFont().getFamily(), fontSize );
                clippedText = Utils.computeClippedText( newFont, label.getText(), label.getWidth(), label.getTextOverrun(), label.getEllipsisString() );
            }

            label.setFont( newFont );
    } );

    Scene scene = new Scene( new VBox(label), 350, 200 );
    primaryStage.setScene( scene );
    primaryStage.show();
}

根据您的要求,您可以简化逻辑并进一步缩短computeClippedText()方法的计算时间.

Based on your requirements you can simplify the logic and improve the calculation time of computeClippedText() method further.

这篇关于如果溢出,请调整JavaFX标签的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆