B4J Question [Solved] Need help with Canvas.DrawText.

Dominik H

Member
Licensed User
I'm at a loss here. I need to dynamically draw text with Canvas.DrawText, and while it works the first time around, when the text is changed it overlaps.
I've tried:
  • Canvas.ClearRect()
  • Canvas.DrawRect() - with fx.Colors.Transparent
  • Setting views invisible and visible again
  • Adding and removing canvas from pane.
Not sure what else to try. Here is what I mean:
cjFOxlX.gif

Code in question::
cnv.DrawText(New, cnv.Width/2, cnv.Height, fx.LoadFont(File.DirAssets, "ITC_Souvenir_LT_Light.ttf", 48), fx.Colors.Black, "CENTER")

My end goal for drawing text is so that I can stretch it out, and modify it. using simply a label does not allow me to achieve this.
 

Daestrum

Well-Known Member
Licensed User
Are you calling cnv.ClearRect(...) each time before the DrawText ?

eg
B4X:
Sub DrawIt(s As String)
    cnv.ClearRect(0,0,cnv.Width ,cnv.Height)
    cnv.DrawText(s,cnv.width/2,cnv.height, fx.DefaultFont(20),fx.Colors.Blue,"CENTER")
End Sub
 
Last edited:
Upvote 0

Dominik H

Member
Licensed User
Are you calling cnv.ClearRect(...) each time before the DrawText ?

eg
B4X:
Sub DrawIt(s As String)
    cnv.ClearRect(0,0,cnv.Width ,cnv.Height)
    cnv.DrawText(s,cnv.width/2,cnv.height, fx.DefaultFont(20),fx.Colors.Blue,"CENTER")
End Sub

Tip: I never use Canvas. B4XCanvas is preferred as it is cross platform.

Not sure what exactly are you trying to do but check BCTextEngine.

Oh the ClearRect() does work, turns out I didn't do 0,0 for coordinates. However new problem. I need to be able to physically stretch this text sideways (as in make it really wide, not scaling font size)

Does BCTextEngine support this?

Previously, I've been able to achieve the stretch effect with B4A Canvas, I don't know why, but setting long widths and short heights in B4A allows the canvas to fully stretch the text to the view. B4J keeps the font size the same, and does not stretch to any bounds but tries to keep the text size the same throughout it seems.
 
Upvote 0

Star-Dust

Expert
Licensed User
I'm at a loss here. I need to dynamically draw text with Canvas.DrawText, and while it works the first time around, when the text is changed it overlaps.
I've tried:
  • Canvas.ClearRect()
  • Canvas.DrawRect() - with fx.Colors.Transparent
  • Setting views invisible and visible again
  • Adding and removing canvas from pane.
Not sure what else to try. Here is what I mean:
cjFOxlX.gif

Code in question::
cnv.DrawText(New, cnv.Width/2, cnv.Height, fx.LoadFont(File.DirAssets, "ITC_Souvenir_LT_Light.ttf", 48), fx.Colors.Black, "CENTER")

My end goal for drawing text is so that I can stretch it out, and modify it. using simply a label does not allow me to achieve this.
Fx.Colors.Transparent doesn't cover just because it is transparent, you have to fill it with the background color (or background image) if you use DrawRec().

But I agree with @Erel, use B4XCanvas.
B4XCanvas.ClearRect (B4XCanvas.TargetRect)
 
Upvote 0

Dominik H

Member
Licensed User
What exactly do you want to do?
I need to be able to physically stretch this text sideways (as in make it really wide i.e. distort it, not scaling font size)
Fx.Colors.Transparent doesn't cover just because it is transparent, you have to fill it with the background color (or background image) if you use DrawRec().

But I agree with @Erel, use B4XCanvas.
B4XCanvas.ClearRect (B4XCanvas.TargetRect)
Can B4XCanvas stretch text (not scale, but stretch as in distorting it)?
 
Upvote 0

klaus

Expert
Licensed User
Can B4XCanvas stretch text (not scale, but stretch as in distorting it)?
Not directly.
You could draw the text with the given size on a small bitmap.
And the copy the bitmap to a wider rectangle.
 
Upvote 0

Dominik H

Member
Licensed User
Not directly.
You could draw the text with the given size on a small bitmap.
And the copy the bitmap to a wider rectangle.
Not something I thought about, but you're absolutely right, that'll be the solution! Thanks Klaus.

Solution:
Sub Process_Globals
    Dim cnv As B4XCanvas
    Dim Pane1 As Pane
    Dim rect As B4XRect
    Dim IMG As ImageView
End Sub
--
Sub DrawText
    rect.Initialize(0, 0, Pane1.Width, Pane1.Height)
    cnv.DrawText(New, BMP.Width/2, BMP.Height - 15dip, fx.LoadFont(File.DirAssets, "ITC_Souvenir_LT_Light.ttf", 48), xui.Color_Black, "CENTER")
    IMG.SetImage(BMP.Snapshot2(fx.Colors.Transparent))
    cnv.ClearRect(rect)
End Sub


I used ClearRect at the end because I didn't care to see the Canvas' text, I only wanted the stretched result which is set to the ImageView.

Not sure if Snapshot2 is needed here, but used it just in case to allow me to set the background to transparent.
 
Upvote 0
Top