iOS Question how to MeasureMultilineTextHeight for B4XFloatTextField?

tzfpg

Active Member
Licensed User
Longtime User
i try to figure out how to MeasureMultilineTextHeight for B4XFloatTextField but no luck,
B4A i can use StringUtils to get B4XFloatTextField height, but B4I try many way but still cannot get the correct height.
B4X:
su.MeasureMultilineTextHeight(TextField.TextField,"YOU ARE THE ROCK!!")

B4I
B4X:
getTextHeight("YOU ARE THR ROCK!!",Font.DEFAULT,txt_msg.TextField.Width)
B4X:
Sub getTextHeight(content As String,fo As Font,LbWidth As Float) As Float
 
    Dim tmpString As String = "大"
    Dim str() As String = Regex.Split(Chr(10),content)
    Dim height As Float
    Dim number As Int
    Dim fontHeight As Float = tmpString.MeasureHeight(fo)
    For Each s As String In str
        number = s.MeasureWidth(fo)/LbWidth + 1
        height = height + number*fontHeight
    Next
    Return height + fontHeight
End Sub

what the right way to measure the height for B4XFloatTextField?
 

Alexander Stolte

Expert
Licensed User
Longtime User
Try this it is B4X:
B4X:
'https://www.b4x.com/android/forum/threads/b4x-xui-add-measuretextwidth-and-measuretextheight-to-b4xcanvas.91865/post-580637
Private Sub MeasureTextHeight(Text As String, Font1 As B4XFont) As Int
#If B4A   
    Private bmp As Bitmap
    bmp.InitializeMutable(1dip, 1dip)
    Private cvs As Canvas
    cvs.Initialize2(bmp)
    Return cvs.MeasureStringWidth(Text, Font1.ToNativeFont, Font1.Size)
#Else If B4i
    Return Text.MeasureHeight(Font1.ToNativeFont)
#Else If B4J
    Dim jo As JavaObject
    jo.InitializeNewInstance("javafx.scene.text.Text", Array(Text))
    jo.RunMethod("setFont",Array(Font1.ToNativeFont))
    jo.RunMethod("setLineSpacing",Array(0.0))
    jo.RunMethod("setWrappingWidth",Array(0.0))
    Dim Bounds As JavaObject = jo.RunMethod("getLayoutBounds",Null)
    Return Bounds.RunMethod("getHeight",Null)
#End If
End Sub
 
Upvote 0
Top