Had to go to bed not getting this to work and what's wrong is still evading me this morning
Basically I would like to set the largest text size possible for Labels and Textbox's. But the views are not created in the designer and so I cannot let the designer and AutoScale take care of it. I've seen an example using StringUtils but was hoping to do this with just core functions. It dawned on me that the problem might be to do with padding around the text and for this I've used JavaObject.
This fixed getting the text to display correctly in portrait mode but it is still too large when in landscape. It seems that if the height of the view is larger than the width then it works correctly but if smaller then the textsize is too large even though the canvas MeasureStringHeight suggests that it should fit?
I've thrown together a small sample (attached), and the code I'm using is shown below.
Basically I would like to set the largest text size possible for Labels and Textbox's. But the views are not created in the designer and so I cannot let the designer and AutoScale take care of it. I've seen an example using StringUtils but was hoping to do this with just core functions. It dawned on me that the problem might be to do with padding around the text and for this I've used JavaObject.
This fixed getting the text to display correctly in portrait mode but it is still too large when in landscape. It seems that if the height of the view is larger than the width then it works correctly but if smaller then the textsize is too large even though the canvas MeasureStringHeight suggests that it should fit?
I've thrown together a small sample (attached), and the code I'm using is shown below.
B4X:
Private Sub maxTextSize(v As View, str As String) As Float
' Get padding values
Dim jo As JavaObject = v
Dim intPaddingLR As Int= jo.Runmethod("getPaddingLeft",Null) + jo.Runmethod("getPaddingRight",Null)
Dim intPaddingTB As Int = jo.Runmethod("getPaddingTop",Null) + jo.Runmethod("getPaddingBottom",Null)
' Set canvas and initialise text size to try
Dim c As Canvas
c.Initialize(v)
Dim fltSize As Float = 70
Dim fltW As Float = c.MeasureStringWidth(str, Typeface.DEFAULT_BOLD, fltSize)
Dim fltH As Float = c.MeasureStringHeight(str, Typeface.DEFAULT_BOLD, fltSize)
' Check if text size will fit, continue reducing until it fits.
Do While (fltW > (v.Width - intPaddingLR)) OR (fltH > (v.Height - intPaddingTB))
fltSize = fltSize - 2
fltW = c.MeasureStringWidth(str, Typeface.DEFAULT_BOLD, fltSize)
fltH = c.MeasureStringHeight(str, Typeface.DEFAULT_BOLD, fltSize)
Log(fltW & " (" & (v.Width - intPaddingLR) & ") " & fltH & " (" & (v.Height - intPaddingTB) & ")")
Loop
Log("Size " & fltSize)
Return fltSize
End Sub