iOS Question label AdjustFontSizeToFit

hanyelmehy

Active Member
Licensed User
Longtime User
i want to change label font and enabel AdjustFontSizeToFit but when i use
B4X:
label.Font=Font.CreateNew2("Digital-7",30)
label.AdjustFontSizeToFit=True

AdjustFontSizeToFit not work ,and label use font size used in Font.CreateNew2
 

hanyelmehy

Active Member
Licensed User
Longtime User
You need to set the font size in the Page_Resize event or it will be reset.
i add label in run time with this code
B4X:
Sub Process_Globals
    Private Vlbl As Label
End Sub

Public Sub Show
    If SPage1.IsInitialized = False Then
           SPage1.Initialize("SPage1")
        SPage1.RootPanel.Color=Colors.Transparent
        SPage1.Title=""
        SPage1.HideBackButton=True
        Vlbl.Initialize("")
        Vlbl.Font=Font.CreateNew2("Digital-7",30)
    End If
    Main.NavControl.ToolBarVisible=False
    Main.NavControl.NavigationBarVisible=False
    Main.NavControl.ShowPage(SPage1)
   
End Sub
Private Sub SPage1_Resize(Width As Int, Height As Int)
    PnlValue.AddView(Vlbl,5,0,PnlValue.Width-10,PnlValue.Height*.66)
    Vlbl.TextAlignment=Vlbl.ALIGNMENT_CENTER
    Vlbl.TextColor=Colors.Blue
    Vlbl.Text="000"
    Vlbl.Color=Colors.Transparent
    Vlbl.Multiline=False
    Vlbl.AdjustFontSizeToFit=True   
End Sub
label still get its size from font.CreateNew2 ,and did not adjust it self to fit label area
 
Upvote 0

hanyelmehy

Active Member
Licensed User
Longtime User
You shouldn't add the view in the Resize event.

Why are you adding the label at runtime?

What is the purpose of setting the font and then calling AdjustFontSizeToFit? It will set the font again unless you changed the label size.

You shouldn't add the view in the Resize event? as i know Resize event is the only event that get actual screen Width and Height ,so i add and adjust label size in this event

Why are you adding the label at runtime?i used to add every thing in runtime ,i know it take more time but its more flexible for me

hat is the purpose of setting the font and then calling AdjustFontSizeToFit? i need to change font type and make label adjust its font size to fill all label area ,so can you please suggest a method to do that

Thank you
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
as i know Resize event is the only event that get actual screen Width and Height ,so i add and adjust label size in this event
That's correct. However the resize event can be raised multiple times. You need to add the views once when the page is created and resize them in the Resize event.

This will be handled automatically if you use the designer to create the layout.

Is the label text cut after you call the code from the first post?
 
Upvote 0

hanyelmehy

Active Member
Licensed User
Longtime User
That's correct. However the resize event can be raised multiple times. You need to add the views once when the page is created and resize them in the Resize event.

This will be handled automatically if you use the designer to create the layout.

Is the label text cut after you call the code from the first post?

Is the label text cut after you call the code from the first post?
text show correctly ,only problem that it use font size from Font.CreateNew2
is there are any other method to adjust font size to fill all label area?
Thank you
 
Upvote 0

hanyelmehy

Active Member
Licensed User
Longtime User
Please create a full example and upload it (File - Export as zip).
i did not know what exactly the problem with this method , the code is straightforward ,i create other method to over come this issue
B4X:
Vlbl.Font=Font.CreateNew2("Digital-7",CheckSize(Vlbl))
Sub CheckSize(lbl As Label) As Int
    Private Checllbl As Label
    Checllbl.Initialize("")
    Checllbl.Visible=False
    Checllbl.RemoveViewFromParent
    SPage1.RootPanel.AddView(Checllbl,0,0,lbl.Width,lbl.Height)
    Checllbl.Text="00000000"
    Private Fsize As Int
    Fsize=20
    Checllbl.Font=Font.CreateNew2("Digital-7",Fsize)
    Checllbl.SizeToFit
    Do Until (Checllbl.Width >= lbl.Width) Or (Checllbl.Height >= lbl.Height)
       Fsize=Fsize+5
       Checllbl.Font=Font.CreateNew2("Digital-7",Fsize)
       Checllbl.SizeToFit   
    Loop
    Checllbl.RemoveViewFromParent
    Return Fsize
End Sub
 
Upvote 0
Top