# Width in Pixels of a StringShape 

**URL:** <https://forum.xojo.com/t/width-in-pixels-of-a-stringshape/46856>\
**Category:** General\
**Created:** [December 30, 2018, 7:32pm UTC](https://forum.xojo.com/t/width-in-pixels-of-a-stringshape/46856 "2018-12-30T19:32:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jon\_Ogden](https://forum.xojo.com/user_avatar/forum.xojo.com/jon_ogden/32/494_2.png) [@Jon\_Ogden](https://forum.xojo.com/u/Jon_Ogden)\
**Post date:** [December 30, 2018, 7:32pm UTC](https://forum.xojo.com/t/width-in-pixels-of-a-stringshape/46856/1 "2018-12-30T19:32:03Z")

</div>

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… 😛

---

<div class="post-metadata">

**Author:** ![Jean-Yves\_Pochez](https://forum.xojo.com/user_avatar/forum.xojo.com/jean-yves_pochez/32/21769_2.png) [@Jean-Yves\_Pochez](https://forum.xojo.com/u/Jean-Yves_Pochez)\
**Post date:** [December 30, 2018, 7:49pm UTC](https://forum.xojo.com/t/width-in-pixels-of-a-stringshape/46856/2 "2018-12-30T19:49:41Z")

</div>

g.stringwidth( s.text)

---

<div class="post-metadata">

**Author:** ![DaveS](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@DaveS](https://forum.xojo.com/u/DaveS)\
**Post date:** [December 30, 2018, 8:00pm UTC](https://forum.xojo.com/t/width-in-pixels-of-a-stringshape/46856/3 "2018-12-30T20:00:59Z")

</div>

> [@419739:@Jean-Yves Pochez](#):
>
> g.stringwidth( s.text)

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

---

<div class="post-metadata">

**Author:** ![Jon\_Ogden](https://forum.xojo.com/user_avatar/forum.xojo.com/jon_ogden/32/494_2.png) [@Jon\_Ogden](https://forum.xojo.com/u/Jon_Ogden)\
**Post date:** [December 30, 2018, 8:52pm UTC](https://forum.xojo.com/t/width-in-pixels-of-a-stringshape/46856/4 "2018-12-30T20:52:15Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![DaveS](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@DaveS](https://forum.xojo.com/u/DaveS)\
**Post date:** [December 30, 2018, 8:56pm UTC](https://forum.xojo.com/t/width-in-pixels-of-a-stringshape/46856/5 "2018-12-30T20:56:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jon\_Ogden](https://forum.xojo.com/user_avatar/forum.xojo.com/jon_ogden/32/494_2.png) [@Jon\_Ogden](https://forum.xojo.com/u/Jon_Ogden)\
**Post date:** [December 30, 2018, 9:05pm UTC](https://forum.xojo.com/t/width-in-pixels-of-a-stringshape/46856/6 "2018-12-30T21:05:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [January 1, 2019, 10:45am UTC](https://forum.xojo.com/t/width-in-pixels-of-a-stringshape/46856/7 "2019-01-01T10:45:10Z")

</div>

> [@419745:@Jon Ogden](#):
>
> But I dont think I can use g.stringwidth without drawing it first, can i?

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.

---

<div class="post-metadata">

**Author:** ![DaveS](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@DaveS](https://forum.xojo.com/u/DaveS)\
**Post date:** [January 1, 2019, 3:46pm UTC](https://forum.xojo.com/t/width-in-pixels-of-a-stringshape/46856/8 "2019-01-01T15:46:03Z")

</div>

```auto
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
```

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 7:30pm UTC](https://forum.xojo.com/t/width-in-pixels-of-a-stringshape/46856/9 "2020-10-29T19:30:37Z")

</div>


