Measuring MeasureStringHeight / Width ?

boten

Active Member
Licensed User
Longtime User
I'm trying to display some text, as big as possible and centered vertically , in landscape mode, but I have some problems.
(never mind the width, the panel on which the text lies will eventually scroll)

I use Sub Measure to determine the biggest safe textsize, up to 3/4 of panel.height (using full panel.height
will only make things worse!)

The entire program follows, along with screen capture (on the emulator).

What am I doing wrong?

B4X:
Sub Process_Globals
End Sub

Sub Globals
Dim lbl As Label
Dim cvs As Canvas
Dim dum As Bitmap
Dim pnl As Panel
Dim phn As Phone
Dim tf As Typeface
End Sub

Sub Activity_Create(FirstTime As Boolean)
  
  tf=Typeface.CreateNew(Typeface.SERIF,Typeface.STYLE_NORMAL)
  
  pnl.Initialize("")
  activity.AddView(pnl,0,0,100%x,100%y)
  pnl.Color=Colors.White
  
  lbl.Initialize("")
  pnl.AddView(lbl,0,0,pnl.Width,pnl.Height)
  
  dum.InitializeMutable(60,60)
  cvs.Initialize2(dum)
  
End Sub

Sub Activity_Resume
Dim f,h,w As Int
Dim txt As String

 phn.SetScreenOrientation(0)  ' landsape only

  txt="Test"
  f=Measure(txt,tf)   ' find "max" text size to fit in panel's height
  
  w=cvs.MeasureStringWidth(txt,tf,f)
  h=cvs.MeasureStringHeight(txt,tf,f)
  pnl.Width=w  
  
  lbl.TextSize=f
  lbl.TextColor=Colors.Blue
  lbl.Typeface=tf
  lbl.Text=txt
  lbl.Width=w
  
  'lbl.Height=h  '<==== if active: This will REDUCE text even more
  lbl.Top=0

  activity.Title=f & "-" & pnl.Height & "-" & lbl.Height & " - " & h
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub Measure(Txt As String,Typ As Typeface) As Int
Dim h,t,p As Int
p=pnl.Height*3/4  ' using ALL of pnl.height will worsen situation
h=12
t=cvs.MeasureStringHeight(Txt,Typ,h)
Do While t<=p
  h=h+1
  t=cvs.MeasureStringHeight(Txt,Typ,h)
Loop
h=h-1
Return h
End Sub
 

Attachments

  • capture.jpg
    capture.jpg
    10.5 KB · Views: 265

boten

Active Member
Licensed User
Longtime User
This doesn't mean that you need to resize the panel. Just clear it and draw the text again.
But once the size of the drawn text (because of longer string, different font, different font size..) is longer than the panel length, the text will NOT be visible on the panel.

So here's my situation:
A) Need to 'draw' (on panel via canvas, or on label) strings of varying:
1) length
2) font (typeface)
3) font size (textsize)
4) color
each of the above 4 parameters can not be determined in advance and are subject to user input.

B) need to scroll the surface (panel) to show the entire string of text. This is done with Animation (though the animation object displays varying speed in long animations)

Any ideas?
* Labels take more vertical space then they should (inter-lines spacing?)

* Drawing directly on the panel via canvas cause memory problems when resizing the panels and re-adjusting the canvs
 
Upvote 0

boten

Active Member
Licensed User
Longtime User
I probably don't fully understand the drawable / mutable concept, so let's c if i understand what u suggest:

1) define a bitmap with
bmp.initializemutable(w,h)

2) "assign" a canvas to it
cvs.initialize2(bmp)

3) draw on bitmap
cvs.drawtext(.....

4) assign bitmap to panel's background
pnl.backgound=bmp

Now, what happens if the text length becomes so long as to be wider then "w" in step 1) ???
Can I "dispose" of the bitmap and redfine it with diferrent width? and if so, how will the canvas behave? will it not cause the same memory problem mentioned earlier?
 
Upvote 0
Top