Other Klaus' SetTextSize snippet

LucaMs

Expert
Licensed User
Longtime User
I tried Klaus' SetTextSize code snippet and, on two EditTexts 40dip and 45dip high (Designer + AutoScaleAll), the text become too high.

I think this is due to the fact that the routine does not take into account the upper and lower padding plus an invisible space of the EditText.

Using the code below I get a better resizing (HiddenLabelPart is an empirical value that represents the invisible part of the EditText but it is functional):

B4X:
Sub Process_Globals
   Dim mStringUtils As StringUtils
End Sub

Sub SetTextSize(lbl As Label, txt As String)
   Dim dt As Float
   Dim limit = 0.5 As Float
   Dim h As Int
 
   Dim TopPadding As Int = getPaddingTop(lbl)
   Dim BottomPadding As Int = getPaddingBottom(lbl)
   Dim HiddenLabelPart As Int = lbl.Height * .15
   If lbl Is Label Then
       HiddenLabelPart = 0
   End If
   Dim TotalToExclude As Int = TopPadding + BottomPadding - HiddenLabelPart
 
   lbl.Text = txt
   lbl.TextSize = 72
   dt = lbl.TextSize
   h = mStringUtils.MeasureMultilineTextHeight(lbl, txt) + TotalToExclude
   Do While dt > limit Or h > lbl.Height
       dt = dt / 2
       h = mStringUtils.MeasureMultilineTextHeight(lbl, txt) + TotalToExclude
       If h > lbl.Height Then
           lbl.TextSize = lbl.TextSize - dt
       Else
           lbl.TextSize = lbl.TextSize + dt
       End If
   Loop
End Sub

'Gets the Top padding of the given view
Sub getPaddingTop(v As View) As Int
   Dim jo = v As JavaObject
   Return jo.RunMethod("getPaddingTop", Null)
End Sub

'Gets the Bottom padding of the given view
Sub getPaddingBottom(v As View) As Int
   Dim jo = v As JavaObject
   Return jo.RunMethod("getPaddingBottom", Null)
End Sub
 

Attachments

  • SetTextSize test.zip
    8.2 KB · Views: 356
Last edited:
Top