Hi Latcho,

I see you've gotten a couple response but I'll throw another one in to the mix. The main functions that will interest you are: 1) getCharIndexAtPoint which locates the char at the edge of the textfield
2) replaceSelectedText which is used to substitute in "..."

This is some code ripped out of a class I use that extends TextField and does a bunch of things for me, one of which is the truncation of text. It is not complete but should give you an idea for approaching this problem.

private var _htmlText:String = ""; //holds the complete text for the field even when truncation occurs private var _tempText:TextField = new TextField();; //another textField which is used to truncate the text and then place it into this actual field private var _gutterCompensation:Number = -5; // -5 compensates for the gutter on either side of the text

private function checkDimensions(){
        _htmlText = super.htmlText;
        if(super.textWidth > width - _gutterCompensation){
        //use the temporary text holder to do the text shortening and replacing
        _tempText.htmlText = _htmlText;
_tempText.defaultTextFormat = super.defaultTextFormat; //this ensures that the "..." are placed using the correct formatting //find the character located at the edge of the text then count back 2 more characters and replace everything from there to the end with "..." _tempText.setSelection(super.getCharIndexAtPoint(width- _gutterCompensation, 2)-2, super.text.length);
        _tempText.replaceSelectedText("...");
        super.htmlText = _tempText.htmlText;
}

Again is this an email/modified version so it is not complete just to give you ideas. (I don't remember if I really needed to use super.htmlText or could have gotten by with just this.htmlText)

Basically what I do is when the text field looses focus after editting I run the checkDimensions function which does my truncation but also stores the complete text. When the user brings the textfield back in to focus I replace the truncated text with the complete text I have stored.

hth,

Rob

On 17-Nov-08, at 4:26 AM, Latcho wrote:

How to break a line of text on the optimal charachter and adding "..." if it doesn't fit a single lined textfield
| I would like to break | this line of text.
| I would like to br... |

Any suggestions (AS3)

Latcho
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to