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