Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Word Wrapping in Canvas !!!

 
Sam Cala
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
I have a canvas in a chat applet where all the messages are being drawn/displayed send by different users. Now, the problem is - the width of a canvas is fixed. Now, the problem is when the user types a message the length of which is more than the canvas width, the message gets cuts when the canvas width exceeds. I want the canvas when the width of message exceeds, the rest of the message should be displayed on the next line. Also, I want the word wrapping feature ?? What should I do ?
I hope there is of course someone to help me !!
Regards and thanx in advance.
 
Dave Turner
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
You will need to use the FontMetrics class to determine the length of strings, using stringWidth(). For each message you will need to split it into sections that are smaller than the width of the canvas and display each one on a new line.
hope this helps
dave
 
Paul Bailey
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi, I tried using the fond metrics class and found out that it doesn't always return the right metrics! which is to say that sometimes the font doesn't display the way the metrics class claims it will. what to do?
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
For fixed sized fonts (i.e., Courier) the font metrics return the correct character sizes. But for proportional fonts (i.e., Helvetica) the font metrics return average character sizes. Since each character is a different width it is impossible to give you an exact answer.
My advice, use fixed sized fonts if your application depends on word wrapping or exact string widths ...
Regards,
Manfred.
 
Greg Harris
Ranch Hand
Posts: 1012
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sam,
i see that this post was resurrected from the dead... i hope you see my reply. i just wrote a bit of code that takes input from the user and displays it in an applet window... if the line is too long, the function searches for the closest " " from the end of the line, cuts the string and then displays the input on 2 lines... my code is pretty rough, but it works. i hacked this out in about an hour, so just use it as a reference.

this code just splits the string one ( 1 ) time, so you will need to modify it if multiple lines are possible. mine does not work perfectly yet, but check here to see what it does... Click here...
feel free to email me with any suggestions!
 
Sam Cala
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Greg, The stuff you mentioned has already been done.
thanks for your inputs.
Dear Manfred Leonhardt,
Whats the way if I'm using Arial as a font, how do I get exact string width ???
Rgds,
 
Greg Harris
Ranch Hand
Posts: 1012
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sam,
i am using arial in the example above. the fontMetrics( font ) function returns the width in pixles of the current font no matter what the font is.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic