iOS Question calculate Height of label for long text

hello

in B4a there is Stringutils library that help user to calculate and set label height depend on text , so there is no problem how long your text or load from internal database or online dynamic data.
after we got the label height we could set other views position on bottom of it easily and be sure that our design will OK in all device and sizes


but i cant find any library or option for do this in B4i . how can i do this ?

thanks ...
 
thank you so much Dear Jan , and sorry for create duplicate

i saw the threat you give me before but not work me in first try , seems there is problem when i use LoadLayout and Designer for label cause when i use
SizeToFit for label that i generate private dim for it from Designer ,the Height of label is still Height was define in Designer and not change ,also the part of my text load in label with ellipsis ... ( like read more )


but when i add all views by code and not use Designer seems your Tips work well
 
Upvote 0
well i put my code here for 2 reason , see if my code is not standard or wrong and maybe be useful for someone like me in future


B4X:
Sub Process_Globals

    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page

    Dim Lbl As Label
    Dim Lb2 As Label
    Dim seprator1 As Panel
    Dim Sc1 As ScrollView  
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"

    Lbl.Initialize("Lbl")
    Lbl.Color = Colors.Yellow
    Lbl.Multiline = True
    Lbl.Text = File.ReadString(File.DirAssets,"my.txt")
    Lbl.Font = Font.CreateNew(20)
    Lbl.Width = 96%x
    Lbl.SizeToFit
  
    Lb2.Initialize("Lb2")
    Lb2.Color = Colors.Gray
    Lb2.Multiline = True
    Lb2.Text = File.ReadString(File.DirAssets,"sample2.txt")
    Lb2.Font = Font.CreateNew(20)
    Lb2.Width = 96%x
    Lb2.SizeToFit
  
    seprator1.Initialize("")
    seprator1.Color = Colors.Blue

    Sc1.Initialize("sc1",98%x,100%y)
    Sc1.Color = Colors.Red
    Page1.RootPanel.AddView(Sc1,1%x,1%y,98%x,98%y)
  
    Sc1.Panel.AddView(Lbl,1%x,0,96%x,Lbl.Height)
    Sc1.Panel.AddView(seprator1,0,Lbl.Height,100%x,20dip)
    Sc1.Panel.AddView(Lb2,1%x,Lbl.Height + seprator1.Height,98%x,Lb2.Height)
  
    Sc1.Panel.Height = Lbl.Height
    NavControl.ShowPage(Page1)
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)
        If Sc1.IsInitialized=True Then
        Sc1.Height=100%y
        Sc1.ContentHeight=Lbl.Height + Lb2.Height +  40dip
        Sc1.ContentWidth=98%x
        Sc1.Width=98%x
        End If

End Sub

these code load 2 different txt file from assets and load them in scrollview , seprator1 is panel that load between them just for fun !


first i was thinking about create new thread about my Questions about scrollview in B4i , but i scared that make another duplicate threat like this one , so i hope someone read this post and give me some info about this

- why we need ContentHeight & ContentWidth for scrollview and not work without them , when Panel.Height is all we need in B4a ?

- whats Page1_Resize , where i can find good info about this like when and how must use it ? and why we must put these code for scrollview in it ?


if you take a look at my code i set

B4X:
Sc1.Panel.Height = Lbl.Height

and it must be wrong value and show only lbl label in scrollview cause the height define wrong but it work perfect , it seems the real code that set the height of inner panel in scrollview is :

B4X:
Sc1.ContentHeight=Lbl.Height + Lb2.Height +  40dip
 
Last edited:
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,

Note: Don't use the code for calculating the label height before the Page wasn't resized. In my opinion the best practice is, to execute the code in the Page_Resize sub.
For the other questions please create a new thread :)

Have a nice weekend

Jan
 
Upvote 0
Hi,

Note: Don't use the code for calculating the label height before the Page wasn't resized. In my opinion the best practice is, to execute the code in the Page_Resize sub.
Jan

i`m sorry but i`m really amateur in B4i , do you have any sample project ? i`m confused about what you saying and which lines must move to Page resize
 
Upvote 0
Top