B4J Question How to dynamically determine height of 'Label' control (with WrapText property set to True) based on its current Text contents?

Magma

Expert
Licensed User
Longtime User
Of course, factoring in the Label's defined/set Font size too.

well... i am using that... ...i always add some dips at the result to be sure...

B4X:
Sub MeasureMultilineTextHeight (Font As Font, Width As Double, Text As String) As Double
    Dim jo As JavaObject = Me
    Return jo.RunMethod("MeasureMultilineTextHeight", Array(Font, Text, Width))
End Sub

#if Java
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javafx.scene.text.Font;
import javafx.scene.text.TextBoundsType;
public static double MeasureMultilineTextHeight(Font f, String text, double width) throws Exception {
  Method m = Class.forName("com.sun.javafx.scene.control.skin.Utils").getDeclaredMethod("computeTextHeight",
  Font.class, String.class, double.class, TextBoundsType.class);
  m.setAccessible(true);
  return (Double)m.invoke(null, f, text, width, TextBoundsType.LOGICAL_VERTICAL_CENTER);
  }
#End If
 
Upvote 0
Top