Android Question Sizing Text

Shadow&Max

Active Member
Licensed User
Longtime User
I know you can determine what size of label you need to display text in B4A, and can adjust the height etc based on that.

Conversely, is there any way to determine the font size needed to completely fill a multiline label, with a fixed height.

For example, say you have a full screen label and some text... but you want to fill that label with the text without empty space below, and without overflowing out of the label... you'd need to change the font size obviously...

Is it possible? If so, how?
 

JakeBullet70

Well-Known Member
Licensed User
Longtime User
Try this, call 'SetText' and pass in your text string and label. It should resize your font to fit the label

I got this code from some where here on the forum.

THIS NEEDS THE StringUtils LIB

B4X:
Public Sub SetText(value As String, mLbl As Label)
    Dim HighValue As Float  = 2
    Dim LowValue As Float = 1
    Dim CurrentValue As Float

    mLbl.Text = value
    Dim multipleLines As Boolean = mLbl.Text.Contains(CRLF)
    'need to set actual start values so find Gross Start/Stop values
    'first is linear Growth with some arbitrary 'large' value 'going with 30
    'this can be fine tuned based on size of the display that you are aiming for/have available vs size of your CustomViews

    Do While Not(CheckSize(HighValue, multipleLines,mLbl))
        LowValue = HighValue
        HighValue = HighValue + 30
    Loop

    CurrentValue = (HighValue + LowValue)/2
    '
    'initial sizes set, now for binary approach
    '
    'adjust this to taste.  I like it at 1, and I think it looks very nice... can go a little larger for even faster times
    'smaller for closer hit to actual optimum, but sacrificing a little speed
    '
    Dim ToleranceValue As Float = .5
    'ToleranceValue = 1

    Dim currentResult As Boolean
    Do While (CurrentValue - LowValue) > ToleranceValue OR (HighValue - CurrentValue) > ToleranceValue
       
        currentResult = CheckSize(CurrentValue, multipleLines,mLbl)
       
        If currentResult Then 'this means currentvalue is too large
            HighValue = CurrentValue
        Else 'currentValue is too small
            LowValue = CurrentValue
        End If
        CurrentValue = (HighValue + LowValue) / 2
    Loop
    Dim offset As Int = 0
    mLbl.TextSize = ((CurrentValue - ToleranceValue) + offset)
    'debugLog("size:"& mLbl.TextSize)
End Sub

'--- returns true if the size is too large
'--- called from setText
Private Sub CheckSize(size As Float, MultipleLines As Boolean, mLbl As Label) As Boolean
    Dim cvs As Canvas ,  bmp As Bitmap
    bmp.InitializeMutable(1%x,1%y)
    cvs.Initialize2(bmp)
    mLbl.TextSize = size
    If MultipleLines Then
        Return su.MeasureMultilineTextHeight(mLbl, mLbl.Text) > mLbl.Height
    Else
        Return cvs.MeasureStringWidth(mLbl.Text, mLbl.Typeface, size) > mLbl.Width OR _
              su.MeasureMultilineTextHeight(mLbl, mLbl.Text) > mLbl.Height
    End If
End Sub
 
Upvote 0

Shadow&Max

Active Member
Licensed User
Longtime User
Thanks Jake... I'll try it out in a few... VERY much appreciated!
 
Upvote 0

Shadow&Max

Active Member
Licensed User
Longtime User
Found the lib... It's Erel's called AutoTextSizeLabel...

Thanks Jake...
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
Is this what you are looking for?

B4X:
Sub SetTextSize(lbl As Label, txt As String)
   Dim dt As Float
   Dim limit = 0.5 As Float
   Dim h As Int
   
   lbl.Text = txt
   lbl.TextSize = 72
   dt = lbl.TextSize
   h = stu.MeasureMultilineTextHeight(lbl, txt)   
   Do While dt > limit OR h > lbl.Height
     dt = dt / 2
     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
 

Attachments

  • LabelTextFitting.png
    LabelTextFitting.png
    50 KB · Views: 1,797
  • LabelTextFitting.zip
    7 KB · Views: 336
Last edited:
Upvote 0

Shadow&Max

Active Member
Licensed User
Longtime User
Indeed it is Klaus... Once again, thank you!!!!
 

Attachments

  • Klaus.PNG
    Klaus.PNG
    126.3 KB · Views: 286
Upvote 0
Top