Test for Label overrun

2019-04-06 03:28发布

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 size. This would hopefully make it so that the label will change size until the entire string is visible.

My problem it that I am not sure how to test to see if the text has overrun. I have tried testing to see if the label's text ends with the ellipse string, but I believe the ellipse is not technically added to the textProperty of the label. So does anyone know of a good way to test for this?

3条回答
混吃等死
2楼-- · 2019-04-06 04:00

This work for me !! In javafx

public class LabelHelper{
  public static boolean necesitaTooltip(Font font, Label label){
    Text text = new Text(label.getText());
    text.setFont(font);
    Bounds tb = text.getBoundsInLocal();
    Rectangle stencil = new Rectangle(
            tb.getMinX(), tb.getMinY(), tb.getWidth(), tb.getHeight()
    );

    Shape intersection = Shape.intersect(text, stencil);

    Bounds ib = intersection.getBoundsInLocal();
    return ib.getWidth() > label.getPrefWidth();
  }

  public static void asignarTexto(Label label, String texto){
      label.setText(texto);
      if (necesitaTooltip(label.getFont(), label)){
          Tooltip tp = new Tooltip(texto);

        label.setTooltip(tp);
      }
  }
} 

Only call a asignarTexto(label, texto) for set text a label and check if the text is overrun in the label then add a tooltip for label.

查看更多
不美不萌又怎样
3楼-- · 2019-04-06 04:03

The short and disappointing answer: You simply cannot do this in a reliable way.

The slightly longer answer is, that the label itself does not even know whether it's overflown or not. Whenever a label is resized, the skin class (LabeledSkinBase) is responsible for updating the displayed text. This class, however, uses a JavaFX utils class to compute the ellipsoided text. The problem here is that the respective method just returns a string that is ellipsoid if this is required by the label's dimensions. The skin itself never gets informed about whether the text was actually ellipsoided or not, it just updates the displayed text to the returned result.

What you could try is to check the displayed text of the skin class, but it's protected. So you would need to do is to subclass LabelSkin, and implement something like that:

package com.sun.javafx.scene.control.skin;

import java.lang.reflect.Field;

import javafx.scene.control.Label;

@SuppressWarnings("restriction")
public class TestLabel extends LabelSkin {
  private LabeledText labelledText;

  public TestLabel(Label label) throws Exception {
    super(label);

    for (Field field : LabeledSkinBase.class.getDeclaredFields()) {
      if (field.getName().equals("text")) {
        field.setAccessible(true);
        labelledText = (LabeledText) field.get(this);
        break;
      }
    }
  }

  public boolean isEllipsoided() {
    return labelledText != null && labelledText.getText() != null && !getSkinnable().getText().equals(labelledText.getText());
  }
}

If you use this skin for you Label, you should be able to detect whether your text is ellipsoided. If you wonder about the loop and the reflection: Java didn't allow me to access the text field by other means, so this may be a strong indicator that you really should not do this ;-) Nevertheless: It works!

Disclaimer: I've only checked for JavaFX 8

查看更多
4楼-- · 2019-04-06 04:05

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);
});
查看更多
登录 后发表回答