Public Sub MeasureMultilineTextHeightLBL(oParent As Activity, lbl As Label, strText As String, oCSB As CSBuilder) As Int
Dim Left, Top, Right, Bottom As Int
Dim edtDummy As EditText
Dim lstLinePos As List
Dim Ht As Int
Dim jo = lbl As JavaObject
'get padding
Left = jo.RunMethod("getPaddingLeft", Null)
Top = jo.RunMethod("getPaddingTop", Null)
Right = jo.RunMethod("getPaddingRight", Null)
Bottom = jo.RunMethod("getPaddingBottom", Null)
'setup temp edittext, same parameters, but twice the height
edtDummy.Initialize("")
oParent.AddView(edtDummy, 0, lbl.Height * -2dip, lbl.Width, lbl.Height * 2)
edtDummy.TextSize = lbl.TextSize
edtDummy.Typeface = lbl.Typeface
edtDummy.Padding = Array As Int(Left,Top,Right,Bottom)
If oCSB.IsInitialized Then
edtDummy.Text = oCSB
Else
edtDummy.Text = strText
End If
DoEvents 'ignore warning, this is needed
lstLinePos = GetEditTextLinePositions(edtDummy, strText, oCSB)
edtDummy.RemoveView
Ht = lstLinePos.Get(lstLinePos.Size - 1) + Bottom
Return Ht
End Sub
Sub GetEditTextLinePositions(edt As EditText, strText As String, oCS As CSBuilder) As List
Dim i As Int
Dim xy() As Int
Dim iYOld As Int
Dim lstLines As List
If oCS.IsInitialized Then
strText = oCS.ToString
End If
lstLines.Initialize
For i = 0 To strText.Length
SetSelection(edt, i, i)
xy = GetXYCursor(edt)
If i = 0 Then
iYOld = xy(3)
End If
If xy(1) > iYOld Or i = strText.Length Then
lstLines.Add(xy(3))
iYOld = xy(3)
End If
Next
Return(lstLines)
End Sub
'Posted by Klaus'
Sub GetXYCursor(edt As EditText) As Int() 'ignore warning
Dim joEditText As JavaObject
Dim joLayout As JavaObject
Dim PaddingLeft As Int
Dim PaddingTop As Int
Dim Pos As Int
Dim Line As Int
Dim Column As Int 'ignore warning
Dim LineBaseline As Int
Dim LineAscent As Int 'ignore warning
Dim LineDescent As Int 'ignore warning
Dim LineTop As Int
Dim LineBottom As Int
Dim LineCount As Int 'ignore warning
Dim LineStart As Int
Dim ScrollX As Int 'ignore warning
Dim ScrollY As Int
Dim xy(4) As Int
joEditText = edt
PaddingLeft = joEditText.RunMethod("getPaddingLeft", Null)
PaddingTop = joEditText.RunMethod("getPaddingTop", Null)
ScrollX = joEditText.RunMethod("getScrollX", Null)
ScrollY = joEditText.RunMethod("getScrollY", Null)
Pos = joEditText.RunMethod("getSelectionStart", Null)
joLayout = joEditText.RunMethod("getLayout", Null)
Line = joLayout.RunMethod("getLineForOffset", Array As Object(Pos))
LineBaseline = joLayout.RunMethod("getLineBaseline", Array As Object(Line))
LineBottom = joLayout.RunMethod("getLineBottom", Array As Object(Line))
LineTop = joLayout.RunMethod("getLineTop", Array As Object(Line))
LineAscent = joLayout.RunMethod("getLineAscent", Array As Object(Line))
LineDescent = joLayout.RunMethod("getLineDescent", Array As Object(Line))
LineCount = joLayout.RunMethod("getLineCount", Null)
LineStart = joLayout.RunMethod("getLineStart", Array As Object(Line))
Column = Pos - LineStart
xy(0) = joLayout.RunMethod("getPrimaryHorizontal", Array As Object(Pos)) + PaddingLeft
xy(1) = LineBaseline + PaddingTop - ScrollY 'base line
'Log("GetXYCursor, xy(1): " & xy(1))
xy(2) = LineTop + PaddingTop - ScrollY 'top of the line
'Log("GetXYCursor, xy(2): " & xy(2))
xy(3) = LineBottom + PaddingTop - ScrollY 'text bottom line
'Log("GetXYCursor, xy(3): " & xy(3))
Return xy
End Sub