|
|
|
#1 |
|
Senior Member
Join Date: Jan 2006
Posts: 100
|
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! |
|
|
|
|
|
#2 |
|
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 | |
|
|
|
|
|
#3 |
|
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?? |
|
|
|
|
|
#4 |
|
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 | |
|
|
|
|
|
#5 |
|
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 | +---------------------------+ Code:
+---------------------------+ | Gamer | | x | +---------------------------+ 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 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 Last edited by badaboom55; 02-25-2010 at 03:26 PM. |
|
|
|
|
|
#6 |
|
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
Code:
+-------------------------+ | The Assasination of | | Jesse James by the ... | +-------------------------+ Coward Robert Ford <--- below the fold text |
|
|
|
|
|
#7 |
|
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 In short: PHP Code:
|
|
|
|
|
|
#8 |
|
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 | |
|
|
|
![]() |
| Tags |
| dynamic, field, text, truncate |
|
||||||
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|