B4J Question Canvas align text issue

bogdanc

Active Member
Licensed User
Longtime User
Hi!

Canvas dosent accept ALIGN parameter??

B4X:
Canvas1.DrawText(20,20,fx.DefaultFont(12),fx.Colors.Blue,"CENTER")

Regards
 

klaus

Expert
Licensed User
Longtime User
B4J exposes only the horizontal alignment.
With the routine below, using a JavaObject,you can also use vertical alignment.
Set Filled = True for 'normal' text.
Setting Filled = False can be used for big sizes.

B4X:
'Draws a text
'Text = text to draw
'x = x coordinate of the reference point
'y = y coordinate of the reference point
'Font = text font name
'Paint =  fx.Colors.Color
'HorizontalAlignment possible values: LEFT, CENTER, RIGHT
'VerticalAlignment     possible values: TOP, CENTER, BASELINE, or BOTTOM
'Filled     True for standard text  False for empty text.
Public Sub DrawText3(cvs As Canvas, Text As String, x As Double, y As Double, Font As Font, Color As Paint, HorizontalAlignment As String, VerticalAlignment As String, Filled As Boolean)
    Private Graph As JavaObject
    Graph = cvs
    Graph = Graph.RunMethod("getGraphicsContext2D", Null)
   
    If Filled = False Then
        Graph.RunMethod("setFont", Array As Object(Font))   
        Graph.RunMethod("setStroke", Array As Object(Color))   
        Graph.RunMethod("setTextAlign", Array As Object(HorizontalAlignment))   
        Graph.RunMethod("setTextBaseline", Array As Object(VerticalAlignment))   
        Graph.RunMethod("strokeText", Array As Object(Text, x, y))
    Else
        Graph.RunMethod("setFont", Array As Object(Font))   
        Graph.RunMethod("setFill", Array As Object(Color))   
        Graph.RunMethod("setTextAlign", Array As Object(HorizontalAlignment))   
        Graph.RunMethod("setTextBaseline", Array As Object(VerticalAlignment))   
        Graph.RunMethod("fillText", Array As Object(Text, x, y))
    End If       
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…