Width in Pixels of a StringShape

I feel like I should know how to do this but I’m having a mental block…

How do you calculate the length in pixels of a string shape? Is s is a string shape, s.text.len gives you the length in characters of the text. How can I convert that to pixels?

Like I said, I should know this but… :stuck_out_tongue:

g.stringwidth( s.text)

only if the font and fontsize of “g” is the same as the string shape

So that would work except I’m going to draw the string shape onto a picture. But if the string is too large for the width of the picture I want to decrease the size of the font until it fits. But I don’t think I can use g.stringwidth without drawing it first, can i?

set the font data for G to same as string shape
measure text size using G, adjust if you need to make it smaller
then take G font data and use it in string shape.

Why not just use DRAWSTRING instead of a string shape? Unless you are going to rotate it in which case the size kinda goes out the window

You know, Dave. That’s a good point. Why don’t I use DRAWSTRING? Perhaps I should…

Yes you can. You could even outside a paint event using the graphics object of a temporary picture.
Or you could calculate string width, find out it will never fit and not draw the string at all.

FUNCTION getStringWidth(s as string, fontname as string,fontsize as single) as double // I think it returns INTEGER on WIndows
dim p as new picture(10,10)
dim g as graphics = p.graphics
g.textfont=fontname
g.textsize=fontsize
return g.stringwidth(s)
END FUNCTION
1 Like