Measuring monospace text height

dagnabitboy

Active Member
Licensed User
Longtime User
Is there a way to measure the line to line text height in B4A for text that is written to a label? (character height plus the whitespace to the top of the next character row). I tried the canvas.measureStringWidth and height for text written to a canvas and the value returned for width is exactly what I need. The value for the height that is returned for characters on a canvas doesn't of course doesn't tell me what the line to line spacing of characters written to a label is.

Any and all help most graciously accepted!
 

stevel05

Expert
Licensed User
Longtime User
This might help

B4X:
   Dim R As Reflector
   R.Target= {The name of your Label without brackets or quotes}
   Lineheight=R.RunMethod("getLineHeight")
   Log(LineHeight)

Select the Reflection library in the Libs tab (4th tab bottom right of the IDE)

I think it gives you what you want if I remember correctly from when I used it. You can check it against the value from the canvas measure string height result.

Documented here
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Take a look at strigsutils...thers a method to get a view text height
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Damn, I didn't find that when I needed it! :(
 
Upvote 0

dagnabitboy

Active Member
Licensed User
Longtime User
Thanks everyone for such quick and thorough replies! I am humbled by how much there is to B4A, and to how much some of you know!! I will post another reply when I get a chance to try these suggestions. Thanks
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I am humbled by how much there is to B4A,

Me too! It seems to be growing exponentially, looking through the list of Libraries is like going to a gadget shop, I didn't know I needed it until I saw it!
 
Upvote 0

dagnabitboy

Active Member
Licensed User
Longtime User
Just wanted to share the results of the suggestions for character height and width. My app is a hex file viewer/editor and I needed to know the exact size of a monospace character font for several different font sizes. My app displays either 8 or 16 hex values depending on the device orientation. My code determines the largest font size that will fit on any given screen.

Here's a screen shot of the hex view:

screen_shot1.jpg


Here is the code I used to determine the width and height of a character. This is not the final code I used, just the test code. If this is useful to you please use it!

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim Rect1 As Rect
   Dim width, height As Float
   Dim t,txt As String
   Dim myname As String
   Dim canvas1 As Canvas
   Dim label1 As Label
   Dim label2 As Label
'   Dim deviceName As PhoneId
   Dim su As StringUtils
   Dim fontsize, textHeight, charheight As Int
   Dim extraDots, charHeight, oneLineHeight, twoLineHeight As Int

' change the fontsize to see what the character width and height are.
   fontsize=10
   
' label1 is for outputting calculated values
   label1.Initialize("label1")
   label1.Color=Colors.Magenta
   label1.TextColor=Colors.Black
   activity.AddView(label1,10,150,300,160)

' label2 is for use with the "MeasureMultilineTextHeight" method.
   label2.Initialize("label2")
   label2.Color=Colors.Magenta
   label2.TextColor=Colors.Black
   activity.AddView(label2,10,350,500,100)

' now use the MeasureStringWidth method to measure the width (including horizontal
' whitespace) of a character.
   Canvas1.Initialize(activity)
   t = "T"
   width = Canvas1.MeasureStringWidth(t, Typeface.monospace, fontsize)
' the following is not needed in the final code, it is used here to show the test
' character on the screen.
   height = Canvas1.MeasureStringHeight(t, Typeface.monospace, fontsize)
    rect1.Initialize(10, 100, 10+width, 100+height)
   Canvas1.DrawRect(Rect1, Colors.White, True, 0)
   Canvas1.DrawText(t, Rect1.Left, Rect1.Bottom, Typeface.MONOSPACE, fontsize, Colors.Blue, "LEFT")
   activity.Invalidate
   
' now we use the "MeasureMultilineTextHeight" to get the character height.  This requires
' two steps because the first line of text placed in a label has extra pixel spacers added.
' there is always 1 extra space below the bottom character, and there are 3,4,5 or more added
' to the top of the first line in a label, depending on font size.
' now get the height of one line
   label2.TextSize=fontsize
   label2.Typeface=Typeface.MONOSPACE
   label2.Text="XXXyy"
   oneLineHeight = su.MeasureMultilineTextHeight(label2,label2.Text)

' now get the height of two lines
   label2.Text="XXXyy"&CRLF&"XXXyy"
   twoLineHeight = su.MeasureMultilineTextHeight(label2,label2.Text)
   label2.Height=twoLineHeight

' now calculate how many extra pixels are added in for this fontsize
' and determine the actual character height (including vertical white space)
   extradots=(2*oneLineHeight-twoLineHeight)
   charHeight=oneLineHeight-extradots    '(2*oneLineHeight-twoLineHeight)

' and last, but not least, show our results.   
   txt   = "font size        = "&fontsize&CRLF
   txt=txt&"one line height  = "&oneLineHeight&CRLF
   txt=txt&"two line height  = "&twoLineHeight&CRLF
   txt=txt&"character height = "&charHeight&CRLF
   txt=txt&"character width  = "&width&CRLF
   txt=txt&"extra spacing    = "&extraDots
   label1.Text=txt
End Sub

And a note to Erel: I will endeavor to improve my search foo!! :)
 
Upvote 0
Top