iOS Question how to calculate correct height of text within fixed width label

AubreyPCR

Member
Licensed User
Longtime User
Hello,
I created a custom scrolling text box in B4A using a label embedded in a scroll view.
I used strUtils to calculate the correct height for the label to enable scolling by setting the internal panel height of the scrollview.
I want to create a similar view in B4I using scrollview.ContentHeight but I cannot calculate the height of the text. I cannot use string.MeasureHeight as it does not take text "wraps" in the label into account. I cannot use SizeToFit because I need to control the width of the label.
Does anyone know a way I can get the real height of multi line text so that I can set the correct label height ?
Thanks
 
Last edited:

JanPRO

Well-Known Member
Licensed User
Longtime User
You can set the width and then call SizeToFit:
B4X:
 Dim Lbl As Label
Lbl.Initialize("Lbl")
Page1.RootPanel.AddView(Lbl,10,10,0,0)
Lbl.Multiline = True
Lbl.Text = "hello," & CRLF & "this is a test"
Lbl.Font = Font.CreateNew(40)
Lbl.Width = 100
Lbl.SizeToFit
Lbl.SetBorder(2,Colors.Green,0)
Log(Lbl.Height)
 
Upvote 0

AubreyPCR

Member
Licensed User
Longtime User
SizeToFit changes the width of the control. I need to maintain a fixed width.
If I adjust the width after SizeToFit the text wraps at the new edge of the control, meaning the height of the text also changes, so lbl.height is no longer the actual height of the current text display.

eg
SizeToFit for "this a test" will make a view approx 20x100, when I adjust the width to 30 I will get text like

this
is
a
test

but the control will still only be 20 high, so you see only the first line. I need to know the new height of the text. str.MeasureHeight just returns the same as if I had used SizeToFit. If I use SizeToFit again it just returns the control to 20x100.
 
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
I hope I have not misunderstood you:

SizeToFit changes the width of the control.
It depends on the content, but SizeToFit never changes the width to a higher value than the width, you have set before:
B4X:
Dim Lbl As Label
Lbl.Initialize("Lbl")
Page1.RootPanel.AddView(Lbl,10,10,0,0)
Lbl.Multiline = True
Lbl.Text = "hello," & CRLF & "this is a test"
Lbl.Font = Font.CreateNew(40)
Lbl.Width = 100
Lbl.SizeToFit
'The new width is now 93.5 and not 100 like before, 
'because such a big value is not needed to display the content.
'Of course you can set the width again to 100 now
Lbl.SetBorder(2,Colors.Green,0)
Log(Lbl.Height)

SizeToFit for "this a test" will make a view approx 20x100, when I adjust the width to 30 I will get text like

this
is
a
test

but the control will still only be 20 high

Set the width before calling SizeToFit.
 
Upvote 0

AubreyPCR

Member
Licensed User
Longtime User
I rewrote using the SizeToFit code you suggested and it worked as you described.
I think I might have had a bug in my content height/label height code giving me incorrect values first time I tried SizeToFit.

Thanks again for responding.
 
Upvote 0
Top