B4J Code Snippet Measure Multiline Text Height

Discussion in 'B4J Code Snippets' started by Erel, Sep 26, 2017.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Measuring the length of multiline text is simple in B4A with StringUtils.MeasureMultilineTextHeight and in B4i by setting the width of a multiline Label and calling SizeToFit.

    There is no similar method available in B4J. One option is to add a label to an AnchorPane and wait for the layout to be set: https://www.b4x.com/android/forum/threads/measure-text.45750/#post-311358

    Another option is to call a non-public API with the following code:
    Code:
    Sub MeasureMultilineTextHeight (Font As Font, Width As Double, Text As StringAs 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
    Usage example:
    Code:
    Dim height As Double = MeasureMultilineTextHeight(fx.DefaultFont(20), 100$" jskldf jslkd fjlk wel;fk we;lfk we;lf k
    erg
    erg
    erg
    er
    gsdfj lksdf jlksd fjlksd f"$
    )
    Log(height)
    Note that if you want to add it to a class then you need to remove the 'static' modifier from the Java code.

    As this is a non-public API it might change in the future. It does work with Java 8 and Java 9 so it is probably be quite safe to use.
     
    Last edited: Dec 26, 2018
    Knoppi, jmon and stevel05 like this.
  2. Misterbates

    Misterbates Active Member Licensed User

    Would this also work in B4A? Would remove the dependency on StringUtils :)
    Or is there some equivalent code that could be used for MeasureText?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    No.

    There is nothing bad with adding StringUtils to your project. It will add about 10k to the APK.
     
    Misterbates likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice