Cancel
Showing results for 
Search instead for 
Did you mean: 

Calculate Text Width

Gears Phenom Gears Phenom
Gears Phenom

Hello all

 

Multiple times I have wished to caculate the text width, for example right now I am trying to create a frame that does a set process and want to display it like a function block with the name of the frame at the very top as centered text, am using vector graphics to layout the function block as needed but it only has the text size and the X and Y position values.

 

Does any body know a way to get the width that the text will take up in pixels so I can then use this as part of a fourmlar to caculate the position of the text to make it centered.

 

In the past I have estimated the width of a standard character and multipled this by the number of characters to varing suscess, mostly when I was working with 3D text before the bounding box size attributes got added.

 

JamesM

3 REPLIES

Re: Calculate Text Width

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor
you could try a display (that shows e.g. self.label)
with display.getBoundingBox you get the width and height and you can center it in your layout
Steffen Bangsow
freelance simulation specialist  
web: www.bangsow.eu
mail: steffen@bangsow.net

Re: Calculate Text Width

Gears Phenom Gears Phenom
Gears Phenom

Hello

 

A display would work but I want to keep the objects using as little memory as needed as there may be a lot of them in the models we are looking to build soon, so using a display would not be benficial and if so then I would wish to turn of the 3D side of it anyway to save on memory.

 

Guess I could have a display in the class library which is used instead to query the size taken up by the text through doing this and then convery the meters to pixels to use it in my method, that way there is only 1 copy of the display with 3D enabled.

 

JamesM

Re: Calculate Text Width

Gears Phenom Gears Phenom
Gears Phenom

Hello

 

Well went through using a display to do so this morning, changing the text and font size and then measuring the size of it in pixels by getting the bounding box size and have got something that sort of works.

 

The issues that I am having is the size of the display is only updated after the method has finished so have had to put in a sleep to ensure the 3D is changed before I measure it, luckily it acceps a really short sleep so this should not really impact how it runs much. Secondly, and more importantly, it only updates this when the 3D window is open for the display and not normally so this is not really a viable approach for what I want.

 

Any more suggestions?

 

JamesM