How do I print a text centered in a Picture?


StringWidth does not exists too.

Last Sequoia / 2025r1

How about TextWidth?

Here’s a short example:

Var p As New Picture(Canvas1.Width, Canvas1.Height)
Var s As String = "Hallo, Welt!"
p.Graphics.FontSize = 20
p.Graphics.DrawText(s, (p.Width - p.Graphics.TextWidth(s)) / 2, (p.Height - p.Graphics.TextHeight) / 2 + p.Graphics.TextAscent)
Canvas1.Backdrop = p

and if you want to write multiple lines of text:

Var p As New Picture(Canvas1.Width, Canvas1.Height)
Var t() As String = Array("Welcome", "to beautifull", "Xojo-Land!")
p.Graphics.FontSize = 20
Var h As Double = p.Graphics.TextHeight
Var y As Double = (p.Height - (t.Count * h)) / 2 + p.Graphics.TextAscent
For Each line As String In t
  p.Graphics.DrawText(line, (p.Width - p.Graphics.TextWidth(line)) / 2, y)
  y = y + h
Next
Canvas1.Backdrop = p

i’d like to recommend to do the drawing in the Paint Event of the OffScreen Canvas:

g.FontSize = 20
Var h As Double = g.TextHeight
Var y As Double = (g.Height - (t.Count * h)) / 2 + g.TextAscent
For Each line As String In t
  g.DrawText(line, (g.Width - g.TextWidth(line)) / 2, y)
  y = y + h
Next

Of course, but AutoComplete feature does not shows it (as you can see in the Screen shot…)

Else I used it ans ask nothing !

Maybe you don’t know this but AutoComplete only offers the options available depending on the code you are using, for example:

does not show TextWidth because is not something available at that position/code, but:

TextWidth is available at this position/code.

2 Likes

Autocomplete only offers methods that return a type that matches your variable.

With no variable, you don’t get the methods that return a double:

With a double variable, you get the methods that return a double:

One trick that you can use is to use call in place of the variable if you aren’t sure what the method returns:

If you do that, it’s almost like viewing the documentation since you get the full list of possible properties and methods:

3 Likes

OK. Autocomplete does not show all “available”…

I forgot the syntax, so I used Offscreen.Graphics.<tab> to get it and it does not comes.

Of course, what I add Str_Width = before Offscreen.Graphics.<tab> TextWidth appeared.
Stupid behavior (because the syntax is shown → window bottom), if I compile, I have hints on where the error lie.

I just wanted to briefly thank you for titling the thread “How do I print a text centered in a picture?” without any indication that this isn’t the actual problem. :confused:

I would also like to thank you for taking the time to give you simple examples of how you can easily solve the challenge “How do I print a text centered in a picture?”, and for not even deeming it necessary to thank me for that. Quite the opposite, you even have to point out that you could have solved the “How do I print a text centered in a picture?” yourself if only the problem you didn’t mention in your first post had existed.

2 Likes

Bah!

And if autocomplete allows you to do something like:

Var OffScreen As New Picture(Canvas1.Width, Canvas1.Height)
Var t As String = "Hello Emile!"
OffScreen.Graphics.FontSize = 20
OffScreen.Graphics.TextWidth

and you try to run the code and you get an error, you will say that is also a bug/bad behavior. Right?

1 Like

That’s why I was suggesting to do call Offscreen.Graphics.<tab>

call throws away the return value so autocomplete offers all of the methods. It’s handy if you forget the syntax but know that a method exists.

5 Likes