I was looking for a way to determine the height of a textview when it has multiline text. This function now makes that possible across platforms.
From B4J I come from this thread:
From B4J I come from this thread:
Measure Multiline Text Height
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...
www.b4x.com
B4X:
Private Sub MeasureMultilineTextHeight(xLabel As B4XView) As Double
#If B4J
'https://www.b4x.com/android/forum/threads/measure-multiline-text-height.84331/#content
Dim jo As JavaObject = Me
Return jo.RunMethod("MeasureMultilineTextHeight", Array(xLabel.Font, xLabel.Text, xLabel.Width))
#Else if B4A
Dim su As StringUtils
Return su.MeasureMultilineTextHeight(xLabel,xLabel.Text)
#Else if B4I
Dim tmpLabel As Label
tmpLabel.Initialize("")
tmpLabel.Font = xLabel.Font
tmpLabel.Width = xLabel.Width
tmpLabel.Text = xLabel.Text
tmpLabel.Multiline = True
tmpLabel.SizeToFit
Return tmpLabel.Height
#End IF
End Sub
#If B4J
#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
#End If
Last edited: