Hi Guys
I'm trying to place a single character dead center on a label using canvas and drawtext.
The issue is, even thou I have set the padding to zero on all four sides, I still get my character a bit off-centre vertically. Horizontal Center is easy to get, as we just set the draw text alignment to "CENTER", but to center it vertically is proving challenging to say the least...
so any help would be very welcomed!!
what I got that "almost" works...
This should be as easy as subtracting the text height from the view's height, and divide that by two... then subtract that value from the views height, right???? apparently not, so... Please Help!!!
I'm trying to place a single character dead center on a label using canvas and drawtext.
The issue is, even thou I have set the padding to zero on all four sides, I still get my character a bit off-centre vertically. Horizontal Center is easy to get, as we just set the draw text alignment to "CENTER", but to center it vertically is proving challenging to say the least...
so any help would be very welcomed!!
what I got that "almost" works...
B4X:
Sub SetlabelText(v As Label, TFace As Typeface, txt As String)
SetPadding(v, 0, 0, 0, 0) ' we set the padding
Private vHeight As Int = v.Height ' the view's Height
Private vWidth As Int = v.Width ' the view's Width
Private tBase As Int ' the Text Base value
If vHeight > vWidth Then 'if it's taller than it is wide
tBase = vWidth ' the text size is calculated relatively to the view's Width
Else 'if it's wider than it is tall
tBase = vHeight ' the text size is calculated relatively to the view's Height
End If
'Now we get the max FontSize possible for tBase
Private CV As Canvas
CV.Initialize(v)
Private x As Int = 0
Do Until CV.MeasureStringHeight("+", TFace, x)>= tBase
x=x+1
Loop
' I want it to be about 70% of tBase
x = Floor(x * 0.7)
'Now I get the final sizes for text height with the calculated font size
Private tHeight As Int = CV.MeasureStringHeight(txt, TFace, x)
'and try to calculate the vertical origin point (x?) for the string
Private yBase As Int = Floor (vHeight - ((vHeight-tHeight)/2)) ' wich clearly eludes me!!!!
Private xBase As Int = Floor(vWidth/2)'this is horizontal origin point, center!
CV.DrawText(txt,xBase, yBase, TFace, x, Colors.Black, "CENTER")
End Sub
This should be as easy as subtracting the text height from the view's height, and divide that by two... then subtract that value from the views height, right???? apparently not, so... Please Help!!!