Test for Label overrun

前端 未结 4 2422
慢半拍i
慢半拍i 2021-02-20 09:58

I have a multiline label whose text has a chance of overrunning. If it does this, I want to decrease the font size until it isn\'t overrunning, or until it hits some minimum siz

4条回答
  •  耶瑟儿~
    2021-02-20 10:27

    minisu posted a way to detect an overrun in this answer: https://stackoverflow.com/a/15178908/9492864

    This way works for all labeled and I tested it on Buttons with JavaFX 8. You can add a listener for example to the needsLayoutProperty:

    labeled.needsLayoutProperty().addListener((observable, oldValue, newValue) -> {
       String originalString = labeled.getText();
       Text textNode = (Text) labeled.lookup(".text"); // "text" is the style class of Text
       String actualString = textNode.getText();
    
       boolean clipped = !actualString.isEmpty() && !originalString.equals(actualString);
    
       System.out.println("is " + originalString + " clipped: " + clipped);
    });
    

提交回复
热议问题