Android Question I need the opposite of AutoTextSizeLabel

techgreyeye

Member
Licensed User
Longtime User
Good day everyone !!

There is a custom class AutoTextSizeLabel which sets the text size to the largest size possible to fill a Label given it's size.

What I need is the opposite of this - I need to set the Label size to the smallest possible size that allows the text to fit at the defined font size.

Does such a thing exist?

Thanks :)
 

angel_

Well-Known Member
Licensed User
Longtime User
Try this:

 
Upvote 0

techgreyeye

Member
Licensed User
Longtime User
Thanks Angel, Just to be clearer...

I have a label with text "Address Line 1" at font size 20. I need to know the minimum width that will accommodate this text on a single line so I can set the smaller labels to be the same width so everything lines up.
 
Upvote 0

techgreyeye

Member
Licensed User
Longtime User
I've worked out a way, it's a bit messy but it works.

I added a hidden label (lblHidden) in the designer with the same font definition as the label I want to get the minimum width for.

B4X:
    Dim su As StringUtils
    Dim startHeight As Int = su.MeasureMultilineTextHeight(lblHidden,"A")
    Dim thisHeight As Int = 100%y
    Dim thisWidth As Int = 0

    Do Until thisHeight = startHeight
        thisWidth = thisWidth + 1
        lblHidden.Width = thisWidth
        thisHeight = su.MeasureMultilineTextHeight(lblHidden,"Address Line 1")
    Loop

After the loop, thisWidth is the minimum width a label must be to fit "Address Line 1" without text wrapping.

If anyone knows of a better way (ideally built in) then please let me know.

Thanks :)
 
Upvote 0
Top