function g.stringWidth

There is the function g.stringWidth () in Xojo Web Edition? On desktop xojo this function. who can help me?

No this function doesn’t exist in Web:
http://documentation.xojo.com/index.php/WebGraphics

Drawing text in a WebApp should be done using HTML whenever it is possible. This will produce better looking text on Retina devices and enable the user to zoom the text in/out.

Works in Web Edition (just tried) :

dim p as new picture(200,200,32) dim w as integer = p.graphics.StringWidth("yada") msgbox str(w)

StringWidth does not exist in the WebGraphics class and is therefore not available in the WebCanvas Paint event. This is because everything is done asynchronously and measuring the string needs to be done on the browser.

If you wanted to use this for alignment purposes, there is a TextAlignment property.

[quote=144170:@Michel Bujardet]Works in Web Edition (just tried) :

dim p as new picture(200,200,32) dim w as integer = p.graphics.StringWidth("yada") msgbox str(w)[/quote]

I do not know how to do big picture, because I do not know how long is the text.
I did this:

Dim dimtext As Integer
 dimtext=len("hello")*7

Can it work?

[quote=144207:@Stefano Basile]I do not know how to do big picture, because I do not know how long is the text.
I did this:

Dim dimtext As Integer
 dimtext=len("hello")*7

Can it work?[/quote]

It will work only for approximately point size 12 System, and since some characters are larger than others, it is only an average.

But you may not have to measure the string width exactly. What are you trying to achieve ?

Stephano, Lets establish some things first, before you go creating a picture.

  1. Where are you trying to use this… in the Paint event of a WebCanvas?
  2. Why is it that you need the width of the string?

There is also the issue of fonts not always installed on the host, so any attempt to measure a string may be futile.

Yes in the Paint Event of a WebCanvas … I need to know the width of the string to be able to align

The method I posted will probably not work since most hosts do not have the fonts installed. Which means it will work only in debug. Using an average width such as what you posted is therefore probably better, although it will not be as precise.

You can still have a very precise measurement without the font installed by building a dictionary with for every character, its stringwidth, measured on your computer through a basic for-next. Then save that data to a file and load it in the Web app when you need it.

But I wonder if using a multiline WebLabel would not be vastly simpler.

Stefano,

There is a TextAlignment property that you can set to help you with this…

http://documentation.xojo.com/index.php/WebGraphics.TextAlignment

I solved. Thank you all. :slight_smile:

I have the same problem. I’ve got e.g. 10 user definable descriptions that will be grouped on the fly by my app in 3 rows and 4 columns (3*3 + 1 = 10 labels). I want to preserve just enough space between the columns and draw a rectangle around the end result.

I don’t know how I’m able to successfully figure out the space I need on my web canvas.

Tips?

[quote=393764:@Arthur van den Boogaart]I have the same problem. I’ve got e.g. 10 user definable descriptions that will be grouped on the fly by my app in 3 rows and 4 columns (3*3 + 1 = 10 labels). I want to preserve just enough space between the columns and draw a rectangle around the end result.

I don’t know how I’m able to successfully figure out the space I need on my web canvas.

Tips?[/quote]
The problem is that your code is on the server and the drawing is taking place on the user’s browser sometime in the future, so it’s not possible to retrieve this value at runtime. Add to that the fact that if the user doesn’t have the font you’ve selected on their computer, the browser will choose something “close”.

The only way to do this currently would be to make a picture and draw that onto your canvas.

Thanks again Greg!