Android Question Better way to TextSize

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I've been working (since last week when Erel suggest I remove a DoEvent) on restructuring my code to not have any DoEvents.

The only place I have them now is in T8TextSize library

Is there a proper way to adjust the text size of a label without having to call DoEvents as T8TextSize does?

BobVal
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I support a lot of device size's so textsize = 20 works ok on my Samsung Tablet but does not work on my Wife's S3

T8TextSize sizes the text to fit.

Let me rephrase that.

A textsize of 20 might work well on my Tablet but on the S3 I would need a textsize of 12 to fit the same information.
 
Last edited:
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I've been using the T8TextSize (https://www.b4x.com/android/forum/threads/t8textsize-library-smart-string.56851/#content) and in the routines to scale the text he is doing DoEvents (see one of the code modules below) to be able to get the current font size

B4X:
'Scale the text size of target view based on new width of view
'The text will be always one line and the aspect ratio will be preserved
'Text size will be increased as long as the height of text is NOT greater than the height of view
'Width : The width of a view before any changes in its dimensions
'TextSize : The text size of a view before any changes in its dimensions
'
'Example :
'Dim T8TextSize1 as T8TextSize
'   T8TextSize1.Initialize
'   T8TextSize1.SingleLineAutoScale(TextView , 100dip , 12)
Public Sub SingleLineAutoScale(TargetView As Object , Width As Int , TextSize As Int)
   Dim lbl As Label = TargetView
     
   
     If getLineCount(TargetView) > 1 Then    
       'should be set maximum text size
       CallSubDelayed3(Me,"SingleLineFitText",TargetView,True)
       Return
     Else
       'is one line so continue
       WidthOfLineBefore = getTextWidth(TargetView , lbl.Typeface ,TextSize)
       WidthRatioBefore = WidthOfLineBefore / Width
     End If   
     
     'set maximum text size
     lbl.TextSize = 72
     Rate = lbl.TextSize
     DoEvents
     
     'measure width of text after and height of text
     WidthOfLineAfter = getTextWidth(lbl , lbl.Typeface ,lbl.TextSize)
     WidthRatioAfter = WidthOfLineAfter / lbl.Width    
     'height of text can not be larger than the height of view
     HeightOfText = getTextHeight(lbl)
     
     'measure line count
     linecount = getLineCount(lbl)
     
     'set text size in a while loop
     Do While Rate > 0.5 Or linecount > 1 Or HeightOfText > lbl.Height Or WidthRatioAfter > WidthRatioBefore
       Rate = Rate / 2   
         If WidthRatioAfter > WidthRatioBefore Or HeightOfText > lbl.Height Or linecount > 1 Then
           lbl.TextSize = lbl.TextSize - Rate
         Else
           lbl.TextSize = lbl.TextSize + Rate
         End If
         DoEvents     
         
         'measure width and height and line count with new text size
         WidthOfLineAfter = getTextWidth(lbl , lbl.Typeface ,lbl.TextSize)
         WidthRatioAfter = WidthOfLineAfter / lbl.Width    
         HeightOfText = getTextHeight(lbl)
         linecount = getLineCount(lbl)   
     Loop
End Sub

Will look at the link you provided
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
If it's just about setting the correct textsizes for several devices, see here: https://www.b4x.com/android/forum/threads/label-utils.39338/
I use this a lot and it works fine for me.


B4X:
Sub SetLabelTextSize(lbl As Label, txt As String, MaxFontSize As Double, MinFontSize As Double)
    Dim FontSize = MaxFontSize As Double
    Dim Height As Int
    Dim stu As StringUtils
  
    lbl.TextSize = FontSize
    Height = stu.MeasureMultilineTextHeight(lbl, txt)
    Do While Height > lbl.Height And FontSize > MinFontSize
        FontSize = FontSize - 1
        lbl.TextSize = FontSize
        Height = stu.MeasureMultilineTextHeight(lbl, txt)
    Loop
End Sub

Play with the padding method, too. Sometimes it looks better when you remove the padding.
 
Upvote 0
Top