[wish] text resize

timo

Active Member
Licensed User
Longtime User
When working with 100%x and 100%y and autoresizing views in percent, it would be nice if text could fit the new dimensions of the views too(labels,etc).

...For Christmas?:D
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
try

B4X:
Sub FindTextSize(BG As Canvas , Text As String, Tface As Typeface , DesiredWidth As Int ) As Int
   Dim temp As Int, LastHeight As Int
   LastHeight=1
   Do Until temp>=DesiredWidth
      temp = BG.MeasureStringWidth(Text, Tface, LastHeight)
      If temp<desiredwidth Then lastheight=lastheight+1
   loop
   Return lastheight
End Sub
 
Last edited:

timo

Active Member
Licensed User
Longtime User
Thank you.
Could you please maybe show me how to implement it in such a code?

B4X:
'Activity module
Sub Process_Globals

End Sub

Sub Globals
   'designer
   Dim pnlDati As Panel
   Dim lblProva As Label
   Dim btnOK As Button
   
End Sub

Sub Activity_Create(FirstTime As Boolean)

activity.LoadLayout("main")
dimensionaLayout

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

'Sub FindTextSize(BG As Canvas , Text As String, Tface As Typeface , DesiredWidth As Int ) As Int
'Dim temp As Int, LastHeight As Int
'LastHeight=1
'Do Until temp>=DesiredWidth
'temp = BG.MeasureStringWidth(Text, Tface, LastHeight)
'If temp<desiredwidth Then 
'lastheight=lastheight+1
'End If
'Loop
'Return lastheight
'End Sub

Sub dimensionaLayout
'adapt to cell screens

Dim H,W As Double
H=100%y 
W=100%x   

Dim pH,pW As Double

pnlDati.Left=0
pnlDati.Top=0
pnlDati.Width=W
pnlDati.Height=H

pH=pnlDati.Height
pW=pnlDati.Width

'Views contaibed in pnlDati:
btnOK.Left=pW*29.28571/100
btnOK.Top=pH*80/100
btnOK.Width=pW*35.71429/100
btnOK.Height=pH*15/100
'...call for textresize

lblProva.Left=pW*14.28571/100 
lblProva.Top=pH*5/100
lblProva.Width=pW*64.28571/100
lblProva.Height=pH*15/100
'...call for textresize

End Sub
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Oki, after you set the width, you just set the textheight to what the function returns

so like

in either Sub Globals or Sub Process_Globals (I forget which, I can check when I get home) you need

B4X:
Dim BG as canvas

In Sub Activity_Create(FirstTime As Boolean) put:

B4X:
BG.initialize(Activity)

after: lblProva.Width=pW*64.28571/100

put: (It may not be textsize/text, I dont have B4A in front of me to check. But you get the idea. It may be caption instead of Text)

B4X:
lblProva.textsize =FindTextSize(BG, lblProva.Text, lblProva.Typeface , lblProva.Width)
 

timo

Active Member
Licensed User
Longtime User
not very nice result :)
I have to add some code to prevent this.
Thank you anyway.
 

Attachments

  • txtsize.jpg
    txtsize.jpg
    7.5 KB · Views: 175

NeoTechni

Well-Known Member
Licensed User
Longtime User
It looks like its adding some whitespace.

Try

B4X:
lblProva.textsize =FindTextSize(BG, lblProva.Text, lblProva.Typeface , lblProva.Width - X)

And just increase the value of X by 1 each run till it works
 
Top