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 & HelpPrograms › Text in a shape
Page Index Toggle Pages: 1
Text in a shape (Read 590 times)
Text in a shape
Jul 4th, 2006, 11:28am
 
Hi all,

What's the best way to put some text inside a particular shape? I'd like to be sure the text isn't going to overflow the shape.

Thanks a lot
Re: Text in a shape
Reply #1 - Jul 4th, 2006, 11:31am
 
Oh, forgot to mention, but I'd like to be able to put the text in the dead centre of the shape.
Re: Text in a shape
Reply #2 - Jul 4th, 2006, 12:55pm
 
Here is a solution for an rectangle Shape:
Quote:


size(300,300);

//drawRect
int w=200;
int h=40;
rect(50,50,w,h);

//setFont and String
PFont font;
font = loadFont("ArialMT-48.vlw");
// set the FontSize to the height of the rect
textFont(font, h);
String s = "The quick brown fox jumped over the lazy dog";

float tS=h;
//the textSize() is shorten by 1 until the textWidth() smaller then the width of the rect
while(textWidth(s)>w){
 tS-=1;
 textSize(tS);
 println(textWidth(s));
}
fill(0);
//draw the text in the middle of the rect
text(s,50+(w-textWidth(s))/2,50+h/2+tS/2);


Re: Text in a shape
Reply #3 - Jul 8th, 2006, 9:16pm
 
Sorry, I forgot to reply to this thread. Thanks for your help. I wasn't very clear, but was actually after the shape being the size of the text, rather than the other way round. However, I was able to kinda "reverse" your solution to get the effect I needed Smiley

Thanks
Page Index Toggle Pages: 1