B4J Question canvas.DrawText2(...) Not Wrapping Text

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
I've been playing this for a while and I can't figure out what I'm doing wrong. When I am using DrawText2 on a canvas, the text is being compressed to fit within the "MaxWidth" parameter, rather than being wrapped onto multiple lines. For example:
B4X:
#Region  Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 400
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
  
    Dim poCan As Canvas
    poCan.Initialize("")
    MainForm.RootPane.AddNode(poCan, 0, 0, MainForm.Width, MainForm.Height)
    MainForm.RootPane.Style =  "-fx-background-color:#000000;"
    Dim poFnt As Font = fx.CreateFont("Verdana", 30, False, False)
    poCan.DrawText2("This is a long line of text that should be wrapped due to the size limit applied.", 10dip, 100dip, poFnt, fx.Colors.White, "LEFT", 150dip)
  
End Sub

The output is:
HtIEQJQ.png


Suggestions?
 

Daestrum

Expert
Licensed User
Longtime User
It does seem to be doing what the API description says, it resizes the text to fit the size you specify. It doesn't mention splitting onto new lines.
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
It does seem to be doing what the API description says, it resizes the text to fit the size you specify. It doesn't mention splitting onto new lines.

Well, I was relying on what the intellisense description noted:
4OyKNyk.png


If that is incorrect then I will experiment with Erel's suggestion.

Thanks!
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Create a label with the text and add it to node tree (it can be outside of the visible area). Call Label.Snapshot to create an Image and then draw it with Canvas.DrawImage

That is kind of working, Erel, and it is wrapping the text now. However, I cannot find out how to make the background of the returned image transparent (so the text can be drawn on top of other things). No matter what I've tried setting the style to (including not setting it at all) the label image background is white.

The new routine:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
   
    Dim poCan As Canvas
    poCan.Initialize("")
    MainForm.RootPane.AddNode(poCan, 0, 0, MainForm.Width, MainForm.Height)
    MainForm.RootPane.Style =  "-fx-background-color:#000000;"
    Dim poFnt As Font = fx.CreateFont("Verdana", 25, False, False)
    Dim psText As String = "This is a long line of text that should be wrapped due to the size limit applied."
    Dim poImg As Image = WrapDrawnText(MainForm.RootPane, psText, fx.Colors.Blue, poFnt, "TOP_LEFT", 150dip)
    poCan.DrawImage(poImg, 10dip, 10dip, poImg.Width, poImg.Height)
End Sub

Public Sub WrapDrawnText(Container As AnchorPane, Text As String, TextColor As Paint, TextFont As Font, TextAlign As String, MaxWidth As Int) As Image
    Dim poImg As Image
    Dim poLbl As Label
   
    'Create a label
    poLbl.Initialize("")
    poLbl.WrapText = True
    poLbl.Font = TextFont  
    poLbl.TextColor = TextColor
    poLbl.Alignment = TextAlign
    poLbl.Text = Text
    poLbl.Style = "-fx-background-color: rgba(0, 0, 0, 0);"
    Container.AddNode(poLbl, -MaxWidth, 0, MaxWidth, -1)
    poImg = poLbl.Snapshot
    poLbl.RemoveNodeFromParent

    Return poImg
End Sub
The Result:
NH4QxFv.png


Any further suggestions?
 
Upvote 0
Top