Return to Snippet

Revision: 10779
at January 13, 2009 12:29 by ghwilson4456


Initial Code
var myCopy:String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras ullamcorper est in nibh. Integer dapibus, nisi at venenatis ullamcorper, metus eros vulputate nisi, in luctus erat sapien a mauris.\n\nSuspendisse dolor ipsum, congue at, commodo id, consectetur vel, dui. Donec vehicula egestas nibh. Aliquam imperdiet ligula a dolor. Praesent nec tortor id orci semper consectetur. Vivamus dictum lorem nec libero. Maecenas a pede. Aliquam felis nulla, adipiscing in, scelerisque et, rhoncus a, ipsum.\n\nPellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Maecenas rutrum augue at nulla. Vivamus egestas ornare sapien. Nunc lobortis. Aliquam auctor urna quis justo. Suspendisse elit orci, gravida eleifend, suscipit ac, porta ultrices, nibh. Morbi dignissim mauris varius magna.";

copy_txt.height = 150; // HEIGHT MUST BE SET BEFORE APPLYING TEXT TO TEXTFIELD.
copy_txt.text = myCopy;

var lastLine:String = copy_txt.getLineText(copy_txt.bottomScrollV - 1);
var visibleLength:uint = copy_txt.getLineOffset(copy_txt.bottomScrollV - 1);

// REMOVE TRAILING SPACES FROM LAST VISIBLE LINE OF TEXT
while(lastLine.substr(-1, 1) == " ")
{
	lastLine = lastLine.substr(0, lastLine.length - 1);
}

var lastLineWords:Array = lastLine.split(" ");

/* IF LAST WORD DOES NOT HAVE A RETURN OR PERIOD AT
   THE END, REPLACE LAST WORD WITH AN ELIPSE */
var lastChar:String = lastLineWords[lastLineWords.length - 1].substr(-1, 1);

if (lastChar != "." && lastChar.charCodeAt(0) != 13 && lastChar.charCodeAt(0) != 10)
{
	lastLineWords[lastLineWords.length - 1] = ". . .";
}


var myNewCopy:String = myCopy.substr(0, visibleLength) + lastLineWords.join(" ");

copy_txt.text = myNewCopy + " "; // SPACE ADDED AT END SO TEXT DOESN'T WRAP UNNECESARILLY.

Initial URL

                                

Initial Description
This code sets the size of a text field and truncates the text to fit, adding an ellipse if necessary.

Initial Title
Text Field Limit with Ellipse

Initial Tags
text

Initial Language
ActionScript 3