any way to know the width/height of displayed text?

boten

Active Member
Licensed User
Longtime User
given the "physical" attributes of a label (font, size....) is it possible to know the width / height of the displayed text?

something like:

B4X:
lbl1.Text="Some Text"
lbl1.typeface=Typeface.CreateNew(Typeface.SERIF,Typeface.STYLE_BOLD_ITALIC)
lbl1.TextSize=32
a=lbl1.TextWidth  <===??????
 

Hubert Brandel

Active Member
Licensed User
Longtime User
Hi,

i have the same problem, I want to CUT the text that it will not wrap in the
next line of a 2 Lable Listview. BUT I have several Windows, so I tried to bring the function out in a code modul. There I get Null Pointer Errors on this code:

B4X:
Sub TextEinpassen( sTxt As String, oTypeFace As Typeface, onTextSize As Float, nMaxWidth As Int ) As String
   Dim nTextWidth As Int
   Dim Canvas1 As Canvas
   Dim nLen As Int
   ' Absichern gegen leere String
   If sTxt = "" Then Return sTxt
   If oTypeFace.IsInitialized = False Then oTypeFace = Typeface.DEFAULT
   nLen = sTxt.Length
   canvas1.i
   Do While sTxt <> "" 
      sTxt = sTxt.SubString2(0,nLen)
      Log("'" & sTxt & "'")
      nTextWidth = Canvas1.MeasureStringWidth( sTxt, oTypeFace , onTextSize ) ' ERROR LINE
      If nTextWidth > nMaxWidth Then
         If nLen > 0 Then
            nLen = nLen -1
         Else
            Exit
         End If
      Else 
         Exit
      End If
   Loop
   Return sTxt.SubString2(0,nLen)
End Sub

I think I have to initialice the canvas, but I don't have the lable here and I can't get a lable from an activity ... so what to do ?
 
Upvote 0

boten

Active Member
Licensed User
Longtime User
The canvas can be intialized to a small bitmap and still measure what you want

B4X:
Dim dum As Bitmap
dum.InitializeMutable(20,20)
canvas1.Initialize2(dum)
 
Upvote 0

Hubert Brandel

Active Member
Licensed User
Longtime User
Hi Klaus,

Initialize it simply to the Activity.
I tried this, but I got an error that 'Variable Activity is used befor ...'
I want this SUB in a code module, so I think I can't do it this way:
B4X:
Dim canvas1 As Canvas
canvas1.Initialize(Activity)
or did I make a syntax error ?

I now use the bitmap to initialize it.
 
Upvote 0

Hubert Brandel

Active Member
Licensed User
Longtime User
Another problem, how to get the size of the labels in a ListView ?
I used the 2 labels way:

B4X:
lvListeLG.Clear '*** this is my Listbox
lvlistelg.Color = Colors.DarkGray
lbl2 = lvlistelg.TwoLinesLayout.SecondLabel ' Info about the second label
lbl2.TextColor = Colors.White
   
lbl1 = lvlistelg.TwoLinesLayout.Label
lbl1.TextColor = Colors.Yellow
lbl1.TextSize  = lbl2.TextSize ' same Text size !
   
' now get the max size:
nMaxSize = lbl1.Width ' thats I thougth, but 
Log("Label-Width: " & lbl1.Width & ", " & lbl2.Width) => -1 -1 ...

' thats what I am doing now :-(
nMaxSize = lvListeLG.Width - 10

Is there a way to get the label width, not the listview width ?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
- Canvas:
Where did you initialize it ?
In Activity_Create ?
I have used it many times.
Have a look here, I used just a few minutes ago.

- Label.Width
A width of -1 means: Match_Parent The view should be as big as its parent (minus padding).
That means the Label is as wide as the ListView.
You can change the width if you want.

Best regards.
 
Last edited:
Upvote 0

Hubert Brandel

Active Member
Licensed User
Longtime User
Hi Klaus,

no I need it in a code module, thanks to all for help, now it works. :sign0060:

B4X:
Sub CutText( sText As String, nTextSize As Float, oTypeface As Typeface, nMaxSize As Int ) As String
   If sText <> "" Then ' Fehler verhindern.
      Dim canvas1 As Canvas
      Dim dum As Bitmap
      dum.InitializeMutable(20,20)
      canvas1.Initialize2(dum)
      If oTypeFace.IsInitialized = False Then oTypeFace = Typeface.DEFAULT
         Dim nTextWidth, i As Int
         i = sText.Length
         Do While i > 0 
            nTextWidth = canvas1.MeasureStringWidth(sText.SubString2(0,i),oTypeFace,nTextSize)
            If nTextWidth >= nMaxSize Then 
               Log("CUT: " & sText.SubString2(0,i) )
               i = i - 1
            Else
               Exit
            End If
         Loop    
      End If 
   Return sText.SubString2(0,i)
End Sub
 
Upvote 0

Hubert Brandel

Active Member
Licensed User
Longtime User
Hi,

I am not shure what you mean, but I prefere to cut the string and use a direct label.text = shortText ...
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Yes, sorry I missed that one line to initialize. That fixed the null pointer problem.

However, my var mTitleWidth is returning a value of 1085.0 on a screen of 480x854 in landscape mode. It should return a much smaller value, the width of the label taken up by "Setup" right?

B4X:
Dim Canvas1 As Canvas
Canvas1.Initialize(Activity)
lbl_Title.Text = "Setup"   
lbl_Title.Width = -2
lbl_Title.TextSize = 24
mTitleWidth = Canvas1.MeasureStringWidth(lbl_Title, Typeface.DEFAULT, 24)
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
Hello, you can try this code. I use it all the time and it works great. Be sure to check the Reflections library for support. Paste this code in a new project and see if it does what you want. With this code you do not have to tell it the size or font, etc.

B4X:
Sub Globals
   Dim TMS2 As Reflector : Dim TMS2text As Label
   TMS2text.Initialize("TMS2text")
   Activity.AddView(TMS2text,0,0,50,10)
End Sub
Sub Activity_Create(FirstTime As Boolean)
   getTextInfo("Get This Texts Info Height And Width")
   getTextInfo("Now this one...")
   getTextInfo("And one more, Loooonnnngggg One..............")
End Sub
Sub getTextInfo(Tmessage As String)
   TMS2text.Visible = True                                   
   Dim Height, Width As Int
   TMS2text.Text = Tmessage
   TMS2text.TextSize = 20
   TMS2text.Width = -2
   TMS2text.Height = -2
   DoEvents
   TMS2.Target = TMS2text
   Width = TMS2.RunMethod("getWidth")
   Height = TMS2.RunMethod("getHeight")
   Msgbox("Height: " & Height & CRLF & TMS2text.Text, "Width: " & Width)
   TMS2text.Visible = False
End Sub
 
Last edited:
Upvote 0
Top