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.
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
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?
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.
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!
Joined: Jan 03, 2001
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,
Joined: Apr 12, 2001
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.