Android Question Autoscale a label by self-made code

chrjak

Active Member
Licensed User
Longtime User
Dear members,

I have a question. It would be very nice if you could help me! There is a Autoscale module on b4x.com but I can't use this in some modules. Also it would be useful to have a second code by hand...
My problem is that sometimes the text is too big and sometimes the textsize is 1, so that you don't see anything...

Now here is my code: (#1)
B4X:
Sub scalelbl1
    Dim bmp As Bitmap
    bmp.InitializeMutable(1dip,1dip)
    cv.Initialize2(bmp)
    Dim size As Float
    For size = 2 To 80
        If CheckSize(size) Then 
    Next
    size = size - 0.5
    If CheckSize(size) Then size = size - 0.5
    Label1.TextSize = size
End Sub

Sub CheckSize(size As Float) As Boolean
        Return cv.MeasureStringWidth(Label1.Text, Label1.Typeface, size) > Label1.Width OR _
            suu.MeasureMultilineTextHeight(Label1, Label1.Text) > Label1.Height
End Sub

#2

B4X:
Do Until cv.MeasureStringWidth("X", Typeface.DEFAULT, size) < 45dip AND suu.MeasureMultilineTextHeight(labelex, "X") < 45dip OR size = 1
    size = size -0.5
    labelex.TextSize = size
    Loop
All code subs get called every time when the text changes
I made a example project: https://www.dropbox.com/s/myvj7zj5do5r9iq/Scale.zip?dl=0
Why it's on Dropbox? So I can update the file if needed!

It would be great if you could help me solving the mistake in my code!
Thanks for reading this entry!

Yours sincerely,
Chris
 

chrjak

Active Member
Licensed User
Longtime User
There is a missing keyword after Then.
Probably Exit.

Other than that the code looks correct.
Thank you for your answer. I forgot that word in my Code example. If you look in my example project you see it is in... but it does work neither :/

If needed I can make a video how it looks on my phone
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
This code works:
B4X:
Sub SetTextSize(lbl As Label)
    lbl.TextSize = 50
    Do Until cv.MeasureStringWidth(lbl.Text, lbl.Typeface, lbl.TextSize) < lbl.Width AND stu.MeasureMultilineTextHeight(lbl, lbl.Text) < lbl.Height OR lbl.TextSize <= 1
    lbl.TextSize = lbl.TextSize - 0.5
    Loop   
End Sub
 
Upvote 0

chrjak

Active Member
Licensed User
Longtime User
hey klaus!

Thank you!!!
On the first view it looks very good! I will test it! Thank you so much!

Best regards
chris
 
Upvote 0
Top