Android Question MeasureStringHeight

Sergey_New

Well-Known Member
Licensed User
Longtime User
To set the label height I use this code
B4X:
Dim cn As Canvas
cn.Initialize(Activity)
lbl.Height=cn.MeasureStringHeight("T", Typeface.DEFAULT,14)
To display correctly, the height of the label must be doubled.
Does it depend on the screen settings?
How to do it right?
 
Solution
To display correctly, the height of the label must be doubled.
This is because this function is only for single-line text.

Use this code snipped:

or use just this 2 lines:
B4X:
Dim su As StringUtils
Return su.MeasureMultilineTextHeight(xLabel,xLabel.Text)
Note that it only works if the label you are using has its final width and the final font size, only then can the correct height be calculated.

Sergey_New

Well-Known Member
Licensed User
Longtime User
The width also needs to be increased.
Attached an example.
 

Attachments

  • test.zip
    3.3 KB · Views: 114
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
To display correctly, the height of the label must be doubled.
This is because this function is only for single-line text.

Use this code snipped:

or use just this 2 lines:
B4X:
Dim su As StringUtils
Return su.MeasureMultilineTextHeight(xLabel,xLabel.Text)
Note that it only works if the label you are using has its final width and the final font size, only then can the correct height be calculated.
 

Attachments

  • test.zip
    3 KB · Views: 106
Upvote 0
Solution

Sergey_New

Well-Known Member
Licensed User
Longtime User
or use just this 2 lines:
Thanks, this code returns the correct label height.
This is because this function is only for single-line text.
In my example, I corrected the value in the constructor to SingleLine=True.
Nothing has changed, the height will still need to be doubled. :(
Tell me, please, how to set the label width correctly?
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
you need to know the maximum amount of space your label can have
Need to add border width?
There is still a question about the height of the one-line label, but this probably goes to the founding father. :)
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Need to add border width?
1709391345686.png

but i don't know what you have in mind, so i can't give you any information
 
Upvote 0
Top