PDA

View Full Version : Change dynamic text alignment


aeris
03-09-2005, 01:08 PM
Hello everybody,

This is what I want to do.. I have a dynamic text box. It is multiline and text is aligned to the center. This box is kinda large, so if the sentence I am loading is just a single line long, I would like to change the alignment to the left. If the sentence is more than one sentence, I want to keep the alignment as it is, in the center.

I thought of using maxChars or textWidth to find out if my text in the box is more than one line, but none works.

Is there a way that I can do that ? I will appreciate any ideas..

Thanks!

tGP
03-09-2005, 01:23 PM
not sure if you can do that, withouth using html txt...

jjbilly
03-09-2005, 01:28 PM
have you tried using TextFormat.getTextExtent().width ?
You can get the right TextFormat using

myFormat = myTextField.getTextFormat()

aeris
03-09-2005, 11:48 PM
Thanks for the suggestions.

have you tried using TextFormat.getTextExtent().width ?
You can get the right TextFormat using

myFormat = myTextField.getTextFormat()

I have been reading about this method, but I am not sure how I can use it. For especially, figuring out how many lines there is in the multiline textfield, or to figure out end and start points for each lines.

I would be very happy if someone can show me an example to use this method..

jjbilly
03-10-2005, 08:43 AM
I've put two bits of code below - both work with a textfield on the stage called test_txt. Set it to Arial 28, border on, and make it about 300 * 180 pixels to see.

The first one measures the text once it's in the text box, and changes alignment accordingly; the second uses getTextExtent to see whether it will be more than one line. Note that they both have to write the text in before setFormat is called though.

/*
*You could use this technique to find how many lines there are, and reformat accordingly.
*/
var format = test_txt.getTextFormat();
test_txt.text = "either a really long piece of text which is more than one line";
if (Math.random()<0.5) {
test_txt.text = "or just this";
}
if (test_txt.textHeight/34>1) {
format.align = "left";
}
test_txt.setTextFormat(format);

/*
*You could use this technique to find how long the text is as one line, and reformat accordingly.
*/
//there needs to be some text already in the field for the next line to work - alternatively, you could just make
//the text format object yourself.
var format = test_txt.getTextFormat();
var text_str = "either a really long piece of text which is more than one line";
if (Math.random()<0.5) {
text_str = "or just this";
}
//
trace(format.getTextExtent(text_str).width+", "+test_txt._width)
if (format.getTextExtent(text_str).width>test_txt._width) {
format.align = "left";
}
test_txt.text = text_str;
test_txt.setTextFormat(format);

It's easy to see how you'd use the first method to get the number of lines of text. On the other hand, if you want to use getTextExtent to find how long a line is, or what word is at the line break, I think you're going to have to start splitting the string up at spaces and use loops to build it back up, measuring the length of each successive string (with getTextExtent) and comparing it to the width of the box... quite a lot to embark on.

Maybe someone else has done something like this already?

alsvik
03-21-2005, 03:39 PM
<P ALIGN="RIGHT"> within the text :rolleyes: