iOS Question [Solved] Bug by change Fontsize ?

D

Deleted member 103

Guest
Hi,

is this a bug? If not, how to fix the problem?

I have this code( here just as an example):
B4X:
Private btnInput As Button

    Log("1.btnInput.CustomLabel.font.name=" & btnInput.CustomLabel.font.name)
    btnInput.CustomLabel.font = xui.CreateFont(btnInput.CustomLabel.font, 20)
    Log("2.btnInput.CustomLabel.font.name=" & btnInput.CustomLabel.font.name)

and this is the log output:
1.btnInput.CustomLabel.font.name=.SFUI-Regular
2.btnInput.CustomLabel.font.name=TimesNewRomanPSMT
Before the change the font has one name and after another, why?

The font name should not change, right?
 
D

Deleted member 103

Guest
It is related to the way iOS handles the default font. You cannot reuse this font name.

Simplest way to change the font size:
B4X:
btnInput.As(B4XView).TextSize = 20
That would be far too simple, but unfortunately it is not.
The text size should depend on the view size.
I use this procedure for that:
B4X:
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
    
    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
    myFont = Font.CreateNew2(label1.Font.Name, 8)
    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
        myFont = Font.CreateNew2(myFont.Name, dt)
        lbl.Font = myFont
        h = lbl.Text.MeasureHeight(myFont)
        w = NewTxt.MeasureWidth(myFont)
        If w >= Width Or h > Height Then
            myFont = myFont.CreateNew2(myFont.Name, dt - 1)
            Exit
        End If
    Loop
    lbl.RemoveViewFromParent
    Return myFont
End Sub
 
Upvote 0
D

Deleted member 103

Guest
1. You can use Label.AdjustFontSizeToFit to make the label width fit the text.
2. Use XUI.CreateFont2. B4XFont is the same as Font in B4i (it is better to always use the cross platform APIs).
Thanks Erel, but these tips don't help me either.
If I use "Label.AdjustFontSizeToFit", then all labels in a page have different text size, and that doesn't look nice.

I need something so that the default text style set in the designer remains after the change.
If that is not possible, then I have to live with it.
 
Upvote 0
D

Deleted member 103

Guest
Try this.
B4X:
myFont = myFont.CreateNew(dt)
That doesn't work either, see these 2 examples:
B4X:
    '1.Example with btnInput with font= AmericanTypewriter-Bold"
    Log("1.btnInput.CustomLabel.font.name=" & btnInput.CustomLabel.font.name)
    'btnInput.CustomLabel.font = xui.CreateFont(btnInput.CustomLabel.font, 20)
    btnInput.CustomLabel.font = btnInput.CustomLabel.font.CreateNew(20)
    Log("2.btnInput.CustomLabel.font.name=" & btnInput.CustomLabel.font.name)
Logs:
1.btnInput.CustomLabel.font.name=AmericanTypewriter-Bold
2.btnInput.CustomLabel.font.name=.SFUI-Regular

B4X:
    '2.Example with btnInput with font= Default"
    Log("1.btnInput.CustomLabel.font.name=" & btnInput.CustomLabel.font.name)
    'btnInput.CustomLabel.font = xui.CreateFont(btnInput.CustomLabel.font, 20)
    btnInput.CustomLabel.font = btnInput.CustomLabel.font.CreateNew(20)
    Log("2.btnInput.CustomLabel.font.name=" & btnInput.CustomLabel.font.name)
Logs:
1.btnInput.CustomLabel.font.name=.SFUI-Regular
2.btnInput.CustomLabel.font.name=.SFUI-Regular
 
Upvote 0
D

Deleted member 103

Guest
Ok, this is the solution that works for me.
B4X:
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
 
Upvote 0
Top