We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › how to get the width of a text
Page Index Toggle Pages: 1
how to get the width of a text (Read 762 times)
how to get the width of a text
Mar 5th, 2010, 9:41pm
 
PFont font = loadFont("AmericanTypewriter-24.vlw");
textFont(font,45);
text("hello", 0, 50);

i want to get the width and height of this text so that i can make a mouse over event




thanks.
Re: how to get the width of a text
Reply #1 - Mar 6th, 2010, 12:09am
 
textWidth()
textAscent()
textDescent()

Reusing textWidth()'s example:
Code:
float y = 85;
String s = "Tokyo";
float sw = textWidth(s);
text(s, 0, y);
float asc = textAscent();
float desc = textDescent();
line(0, y + desc, sw, y + desc);
line(0, y - asc, sw, y - asc);
Re: how to get the width of a text
Reply #2 - Mar 6th, 2010, 6:36am
 
in this example u hv given it doesnt consider the size and type of the font isnt it
i want to get the width of the text with respect to the font and the size i have specified.
i also found a old post by fry
Back to top
 
 
Re: how to get the width of a text
Reply #3 - Mar 6th, 2010, 7:19am
 
ignore my earlier post.
thanks PhiLho

that solved my problem
Re: how to get the width of a text
Reply #4 - Mar 6th, 2010, 9:27am
 
For the record (since you understood yourself), indeed my code was partial, you needed the code from the example, loading the font and using textFont() to set the text size. textXxxx() calls then refer to latest textFont() call.
Page Index Toggle Pages: 1