Private Sub SetLabelTextSize(label1 As Label, txt As String, Multiline As Boolean) As Font
Dim dt As Float
Dim lbl As Label
Dim myFont As Font
Dim IsFontRegular As Boolean = label1.Font.Name.StartsWith(".SFUI")
lbl.Initialize("")
lbl.SetLayoutAnimated(0, 1, 0, 0, Width, Height)
myRootPanel.AddView(lbl, 0, 0, Width, Height)
lbl.Multiline = Multiline
lbl.Visible = False
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.Text = txt
If IsFontRegular Then
myFont = getDefaultFont(label1.Font, 8)
Else
myFont = Font.CreateNew2(label1.Font.Name, 8)
End If
'Log("myFont.Size= " & myFont.Size & " Fontname=" & myFont.Name)
lbl.Font = myFont
dt = lbl.Font.Size
h = lbl.Text.MeasureHeight(myFont)
w = lbl.Text.MeasureWidth(myFont)
Do While h <= Height
dt = dt + 1
If IsFontRegular Then
myFont = getDefaultFont(myFont, dt)
Else
myFont = Font.CreateNew2(myFont.Name, dt)
End If
lbl.Font = myFont
h = lbl.Text.MeasureHeight(myFont)
w = NewTxt.MeasureWidth(myFont)
If w >= Width Or h > Height Then
If IsFontRegular Then
myFont = getDefaultFont(myFont, dt - 1)
Else
myFont = myFont.CreateNew2(myFont.Name, dt - 1)
End If
Exit
End If
Loop
lbl.RemoveViewFromParent
Return myFont
End Sub
Private Sub getDefaultFont(myfont As Font, size As Float) As Font
Select myfont.Name
Case ".SFUI-Regular"
Return myfont.CreateNew(size)
Case ".SFUI-Semibold"
Return myfont.CreateNewBold(size)
Case ".SFUI-RegularItalic"
Return myfont.CreateNewItalic(size)
Case Else
Return myfont.CreateNew2(myfont.Name, size)
End Select
End Sub