To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Actionscript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 02-24-2010, 08:01 PM   #1
badaboom55
Senior Member
 
Join Date: Jan 2006
Posts: 100
resolved [RESOLVED] Truncate Text in Dynamic Text Field?

Hello All,

I have a Dynamic Text Field placed on stage at authortime.
I need to truncate the text within that field by replacing the last VISIBLE word with an ellipse.
Now, the problem I'm encountering is that in a multiline text box with wrapping turned on, I can't figure out a way to get the index of the last visible character, or some other point where I can start truncating.
Part of the issue is that if you pass a really long SINGLE line string into the text field, Flash will wrap it like you would expect, however, the text gets masked if the text field isn't big enough to display it all, but the text that ISN'T visible still is factored into the values returned by AS3.0

For example, let's say the text field is only big enough to display 3 lines of text.
If the long string I'm trying to display has to wrap 17 times, then myTextField.numLines would return 17 because the unseen text still exists... it's just not able to be displayed in the text field.

Also, myTextField.bottomScrollV doesn't help because the string is all 1 line, so even though you can't see the ENTIRE line, it still returns as visible.

So, does anyone know how to figure out which is the last visible character in a text box?

Everything I've seen about truncating text involves coming up with some arbitrary max character amount, and just truncating at that point.
I would prefer to figure it out dynamically based on the text field's width and height because:
1) character sizes/widths differ for every font, and
2) There may actually be several different fonts used in the same text field.

Thanks!
badaboom55 is offline   Reply With Quote
Old 02-25-2010, 11:30 AM   #2
cancerinform
Mod
 
Join Date: Mar 2002
Location: press the picture...
Posts: 12,744
Here is how you get the visible text at a certain position. I used a button in this example and scrolled the text.

tf.text = "I have a Dynamic Text Field placed on stage at authortime. I need to truncate the text within that field by replacing the last VISIBLE word with an ellipse. Now, the problem I'm encountering is that in a multiline text box with wrapping turned on, I can't figure out a way to get the index of the last visible character, or some other point where I can start truncating. Part of the issue is that if you pass a really long SINGLE line string into the text field, Flash will wrap it like you would expect, however, the text gets masked if the text field isn't big enough to display it all, but the text that ISN'T visible still is factored into the values returned by AS3.0";
var myText:String = tf.text;
but.addEventListener (MouseEvent.CLICK, ch);
function ch (e:Event)
{
var myChar:int = tf.getCharIndexAtPoint(300,40);
trace (myText.charAt(myChar));
}
__________________

- The right of the People to create Flash movies shall not be infringed. -
| www.Flashscript.biz | Help a little girl, Ana, who has cancer. | Flashscript Biz Classes/Components | The new Event design pattern | Clothing for Haiti |
cancerinform is offline   Reply With Quote
Old 02-25-2010, 12:33 PM   #3
badaboom55
Senior Member
 
Join Date: Jan 2006
Posts: 100
Interesting... I missed the getCharAtIndex method...

But, I guess I'm a little confused about your example, where do the values "300" and "40" come from?

Another problem is that I can't really "hardcode" an x, y point to check because different fonts and characters will take up different amounts of space within the text field... so if I say "I'm always going to check this x,y point", there's no garauntee that a character will be in at that point. Also, if the text box isn't quite tall enough to fit an entire line, Flash won't display half a line of text, so it'll just not display that line, and there will be a larger margin between the last visible text and the bottom of the Text Field.

Now, the text field's width and height WILL NOT change... so I guess I need a way to dynamically calculate the x,y point using the text field's width/height/format and any other metrics I have access to.

I suppose I could start checking if there is a character exactly at the bottom-right corner of the Text Field (which there never will be), and then just start moving the x,y point incrementally towards the top-left corner of the Text Field until I run into a character?

Is that a viable solution?

It seems the TextLineMetrics object offers a lot of metrics to use... would any of those be helpful??
badaboom55 is offline   Reply With Quote
Old 02-25-2010, 12:47 PM   #4
cancerinform
Mod
 
Join Date: Mar 2002
Location: press the picture...
Posts: 12,744
The x and y are the position from the left upper corner of the textfield. There will always be a letter or space in case there is no letter and it is a fixed position in the textfield. You can get a longer string by getting the letters before or after the position using the text in the textfield as the starting string.
__________________

- The right of the People to create Flash movies shall not be infringed. -
| www.Flashscript.biz | Help a little girl, Ana, who has cancer. | Flashscript Biz Classes/Components | The new Event design pattern | Clothing for Haiti |
cancerinform is offline   Reply With Quote
Old 02-25-2010, 03:21 PM   #5
badaboom55
Senior Member
 
Join Date: Jan 2006
Posts: 100
hmmm, I'm sorry, I'm not following or I might not have communicated what I'm trying to do properly.

I have a text field on stage with a given width and height.
I need to be able to populate that text box with a variety of text strings.... such as a movie title. Then, once the string is in there, (or before I populate the TextField with the string) I need to truncate it.

SO, if the movie title is "Gamer"... no truncation needed. However, if the movie title is "The Assassination of Jesse James by the Coward Robert Ford"... I'm going to need to truncate the title properly.

You say that "There will always be a letter or space in case there is no letter and it is a fixed position in the textfield"
I don't see how that is true, please explain.

If this is my text field, where "x" is the arbitrary 300, 40 point you just defined:

Code:
+---------------------------+
|                           |
|                         x |
+---------------------------+
If the title is "Gamer", the field will look like this:

Code:
+---------------------------+
| Gamer                     |
|                         x |
+---------------------------+
getCharIndexAt("x point") will return as "-1" telling me there is no character there... whitespace or otherwise, so truncation is not needed.

However, now I change the text via AS3 to "The Assassination of Jesse James by the Coward Robert Ford".

If the text field looked like this:

Code:
+------------------------+
| The Assasination of    |
| Jesse James by the Cow |
+------------------------+
ard Robert Ford                <--- below the fold text
Testing the "x point" would be fine, because it would return the index of the letter "w" in the word "Coward".
However, that's not how Flash wraps words, so in reality the text field would look like this, because Flash won't hyphenate "Cow-ard", so it puts the entire word on the next line, which extends "below the fold":

Code:
+-------------------------+
| The Assasination of     |
| Jesse James by the    x |
+-------------------------+
Coward Robert Ford          <--- below the fold text
So testing the "x point" would return as "-1", but I still need to truncate the title, so choosing an arbitrary point doesn't help... unless I'm completely missing something?

Last edited by badaboom55; 02-25-2010 at 03:26 PM.
badaboom55 is offline   Reply With Quote
Old 02-25-2010, 05:11 PM   #6
badaboom55
Senior Member
 
Join Date: Jan 2006
Posts: 100
To clarify further... I would want the text box to look like this:

Code:
+-------------------------+
| The Assasination of     |
| Jesse James by the ...  |
+-------------------------+
                                    <--- NO BELOW THE FOLD TEXT
Although, I suppose if I absolutely couldn't get the above, this would be acceptable as well, although, I fail to see how this would be possible and the above wouldn't:

Code:
+-------------------------+
| The Assasination of     |
| Jesse James by the ...  |
+-------------------------+
 Coward Robert Ford        <--- below the fold text
badaboom55 is offline   Reply With Quote
Old 02-25-2010, 06:09 PM   #7
badaboom55
Senior Member
 
Join Date: Jan 2006
Posts: 100
I swear to all that was mighty, that this wasn't working when I posted... now it is!

Ok, anyway, if this is the text box:

Code:
+-------------------------+
| The Assasination of     |
| Jesse James by the      |
+-------------------------+
Coward Robert Ford          <--- below the fold text
First I get the text from the last line, using bottomScrollV, and if then I can replace the last word/whitespace character with "..." and then append that to the end of the first line

In short:

PHP Code:
var LastLineText_str:String = myTextField.getLineText(myTextField.bottomScrollV - 1);

// this gets me "Jesse James by the " in a string
// Then just run an operation to replace "the" or the " " character with an ellipse
badaboom55 is offline   Reply With Quote
Old 02-25-2010, 09:18 PM   #8
cancerinform
Mod
 
Join Date: Mar 2002
Location: press the picture...
Posts: 12,744
Well, the magic did its job
__________________

- The right of the People to create Flash movies shall not be infringed. -
| www.Flashscript.biz | Help a little girl, Ana, who has cancer. | Flashscript Biz Classes/Components | The new Event design pattern | Clothing for Haiti |
cancerinform is offline   Reply With Quote
Reply

Tags
dynamic, field, text, truncate

Go Back   Flash Kit Community Forums > Flash Help > Actionscript 3.0

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:55 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.