Android Question Need Help With This Text Sizing Code

Shadow&Max

Active Member
Licensed User
Longtime User
Hi all...

The basis of the code below was posted by Klaus, quite a while ago. It's purpose is to dynamically size text for a multi-line label. I've screwed around with it a lot, primarily because the first iteration Klaus posted worked sometimes, not others. In some cases, the last line of the text was below the visible label space, and sometimes, only a portion of the text is displayed. I also put in some code to detect Carriage Returns and Line Feeds (CRLFs) because they definitely threw it off too.

The label sizes vary quite a bit based upon device, so they're resized when needed.

I really need this code to work, and I've messed with it and stared at it for so long now that I've lost objectivity. The idea is to pass changing text to a label, the size of which may have changed via orientation change, and the text has to fit and be completely shown. Any help would be really appreciated... I have tried different starting font sizes, to no avail. I'm stuck.

B4X:
Sub SetTextSize(lbl As Label, txt As String)
    Dim dt As Float
    Dim limit = 1 As Float
    Dim h As Int

    'This loop determines how many CRLFs there are in the text.
    Dim charcnt As Double
    Dim x2, placer, placerct, ts As Int
         For x = 0 To txt.Length -1
            placerct = 0
            charcnt = txt.Length
                For x2 = 0 To charcnt
                    placer = txt.IndexOf2(Chr(10),x2)
                        If placer = -1 Then
                            Exit
                        Else
                            placerct = placerct + 1   
                            x2 = placer
                        End If
                Next
        Next
        If placerct > 0 Then
            ts = 19 - (placerct * 2)
        Else
            ts = 24
        End If
   
    'Now, do the resizing:
    lbl.Text = txt
    lbl.TextSize = ts
    dt = lbl.TextSize
    h = stu.MeasureMultilineTextHeight(lbl, txt)   
        Do While dt > limit OR h > lbl.Height
            dt = dt / 2.25
            h = stu.MeasureMultilineTextHeight(lbl, txt)   
                If h > lbl.Height Then
                    lbl.TextSize = lbl.TextSize - dt
                Else
                    lbl.TextSize = lbl.TextSize + dt
                End If
        Loop
End Sub
 

Shadow&Max

Active Member
Licensed User
Longtime User
Sure... Give me a few minutes to make one up... Thanks Klaus...
 
Upvote 0

Shadow&Max

Active Member
Licensed User
Longtime User
OK, here's one... fairly simple, not perfect... Various lines of text. For some, in landscape they're fine, in portrait not. Others fine in portrait, not in landscape...

Also, if you change the font style to bold in the designer, it'll all be completely different. Maybe the StringUtils lib is messing up???
 

Attachments

  • KlausText.zip
    9.3 KB · Views: 115
Upvote 0

Shadow&Max

Active Member
Licensed User
Longtime User
Thanks Klaus... Yes, I think it's the function because your calculations seem to be clear and fairly straightforward. BTW, I didn't mean to infer in the initial post that this had anything to do with your code... I just tried it so many different ways with no correct resolution.

I'm glad at least you were able to reproduce it as well... Hopefully Erel will jump in here soon... I use this in 4 portions of my app, and for what I'm doing, it's imperative that it works.

It also doesn't matter what size the label is... I've tried making it less tall, and while it shortens the label, the text is still displayed incorrectly.

Thanks again Klaus... always appreciated...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I see the issue with some of the texts.
If you set the label vertical alignment to center then you can see that the issue seems to be related to letters like T or h which go above other characters:

upload_2014-10-7_8-43-56.png


The measurement is done with a call to a native method. I don't see how it can be changed to return more accurate result.
As I see it you have two options:
1. Add an empty line to all messages at the end.
2. Use a scrollable solution such as CustomListView.AddText.
 
Upvote 0

Shadow&Max

Active Member
Licensed User
Longtime User
I see what you're saying Erel... OK, I'll try adding a line to everything and see how it does... scrolling won't work for me.

Back to the drawing board I guess...

Thanks Erel (and Klaus)

OK, the response above didn't post for some reason so I'm adding to it before I post it.

Thanks Erel.. That worked! Klaus... I took out all of that code I created to jump over CRLFs and reverted back to your original code, which works perfectly as long as I add the extra CRLF to the end of the text before it's processed by the sub.

Thank you both... Cheers!
 
Upvote 0
Top