Sub Class_Globals
    Private bmp As Bitmap
    Private cvs As Canvas
    Private stu As StringUtils
    Private dt As Float
    Private h, Height As Int
    Private w, Width As Int
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    bmp.InitializeMutable(2, 2) 'ignore
    cvs.Initialize2(bmp)
End Sub
#Region TextSize vom Label, EditText und Button ändern
The "Scale" parameter is not used for buttons!
Public Sub SetTextSize(obj As View, txt As String, scale As Float, singleline As Boolean)
    'Log("SetTextSize: txt=" & txt & " :obj=" & obj)
    'WICHTIG! Object muss initialisiert sein!
    If Not(obj.IsInitialized) Then Return
    'WICHTIG! Die Width darf nicht weniger als 30dip sein!
    If obj.Width < 30dip Then Return
'    Dim p() As Int = obj.Padding
'    Log((p(0) + p(2)) / GetDeviceLayoutValues.Scale) '23dip
'    Log("obj.Height=" & obj.Height)
   
    setSingleLine(obj, singleline)
    If obj Is Button Then
        Dim btn As Button = obj
        Height = btn.Height - 14dip
        Width = btn.Width - 10dip
        SetButtonTextSize(btn, txt)
    Else
        Dim lbl As Label = obj
        Height = lbl.Height * scale
        Width = lbl.Width - 5dip
       
        SetLabelTextSize(lbl, txt)
    End If
End Sub
'Sets the TextView to single line
Public Sub setSingleLine(TextView As View, SingleLine As Boolean)
    Dim jo = TextView As JavaObject
    jo.RunMethod("setSingleLine", Array As Object(SingleLine))
End Sub
Private Sub SetButtonTextSize(btn As Button, txt As String)  
    btn.TextSize = 6
    dt = btn.TextSize
    h = stu.MeasureMultilineTextHeight(btn, txt)
    w = cvs.MeasureStringWidth(txt, btn.Typeface, dt)
    Do While h <= Height
        dt = dt + 1
        btn.TextSize = dt
        h = stu.MeasureMultilineTextHeight(btn, txt)
        w = cvs.MeasureStringWidth(txt, btn.Typeface, dt)
       
        If w >= Width Or h > Height Then
            btn.TextSize = dt - 2
            Exit
        End If
    Loop
End Sub
Private Sub SetLabelTextSize(lbl As Label, txt As String)
    Dim s() As String = Regex.Split(CRLF, txt)
    Dim NewTxt As String = txt
    If s.Length > 1 Then
        Dim l As Int
        For i = 0 To s.Length - 1
            If l < s(i).Length Then
                l = s(i).Length
                NewTxt = s(i)
            End If
        Next
    End If
    lbl.TextSize = 6
    dt = lbl.TextSize
    h = stu.MeasureMultilineTextHeight(lbl, txt)
    w = cvs.MeasureStringWidth(txt, lbl.Typeface, dt)
    Do While h <= Height
        dt = dt + 1
        lbl.TextSize = dt
        h = stu.MeasureMultilineTextHeight(lbl, txt)
        w = cvs.MeasureStringWidth(NewTxt, lbl.Typeface, dt)
'        Log(txt & ": w=" & w & " ;h=" & h)
'        Log(txt & ": Width=" & Width & " ;Height=" & Height)
   
        If w > Width Or h > Height Then
            lbl.TextSize = dt - 1
            Exit
        End If
    Loop
End Sub
#End Region