forked from as3/as3-utils
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtruncateText.as
More file actions
145 lines (124 loc) · 4.57 KB
/
truncateText.as
File metadata and controls
145 lines (124 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package utils.textField
{
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
/**
* Truncate a single-line TextField to a specific width.
* @param tf the textfield to truncate.
* @param maxWidth the desired text width to truncate at.
* @param isEllipsed denotes whether to truncate with the ellipse char "...".
* @param isLineEllipse gives the option to "ellipse" the entire width of the field.
*/
public function truncateText(tf:TextField, maxWidth:Number, isEllipsed:Boolean = true, isLineEllipse:Boolean = false):void
{
var ellipse:String = '...';
// save the autosize settings for renewal at the end ...
var autoSizeSetting:String = tf.autoSize;
tf.autoSize = TextFieldAutoSize.NONE;
// subtract a touch of length from the max width to insure no visual overflow ...
//maxWidth = (!isLongEllipse) ? (maxWidth - 5) : (maxWidth -= 10);
// don't ellipse if we fit under the max width.
if (tf.textWidth <= maxWidth && !isLineEllipse)
{
return;
}
// hide the field momentarily so the user doesnt see our tests ...
//tf.visible = false;
// save the original text string & it's length
var str:String = tf.htmlText;
var strLength:Number = str.length;
// save the (stereotypical fonts) widest character's width: "W"
tf.htmlText = "W";
var wCharWidth:Number = tf.textWidth;
// save the (stereotypical fonts) medium character's width: "A"
tf.text = "A";
var aCharWidth:Number = tf.textWidth;
// the max num of (the largest) chars which our max desired width can hold.
var ptr:Number = Math.floor(maxWidth / wCharWidth);
// collect the portion of the string which represents the max large chars (ptr) and add it to the field.
var checkStr:String = str.substr(0, ptr);
tf.text = checkStr;
// Now that we have a baseline set below our maxWidth we can start adding/testing characters:
// in this way we optimize the number of loops which must be performed which *greatly* improves
// performance rather than adding/testing thru the entire string (this is especially important
// when using the method on many long texted TextFields: smart, huh?
var cnt:Number;
while (tf.textWidth < maxWidth)
{
// see if we can fit one or more standard characters in the space remaining: if we cant: bail.
cnt = Math.floor((maxWidth - tf.textWidth) / aCharWidth);
if (cnt == 0)
{
break;
}
// increase the max chars: if we accidentally went above the str length: fix er up ...
ptr += cnt;
ptr = (ptr > strLength) ? strLength : ptr;
// once again set a slice of our max fitting chars to the field.
checkStr = str.substr(0, ptr);
tf.htmlText = checkStr;
// if our max fitting chars is the same as the string length: bail too.
if (strLength == ptr)
{
break;
}
}
// if we want to ellipse apply it to the field.
if (isEllipsed)
{
tf.appendText(ellipse);
}
// again check if we've exceeded the max desired width ...
if (tf.textWidth > maxWidth)
{
// if we have remove a character and test again.
while (tf.textWidth > maxWidth)
{
checkStr = checkStr.substr(0, -1);
tf.htmlText = isEllipsed ? (checkStr + ellipse) : checkStr;
}
}
// alright - things fit perfectly now! However; maybe you want it to
// "ellipse" thru the entire length of the field for visual effect?
//
// TODO - Warning: this functions but is not perfect: can be optimized and made more accurate.
//
if (isLineEllipse)
{
var tmpField:TextField = new TextField();
tmpField.width = tf.width;
tmpField.setTextFormat(tf.getTextFormat());
// save the (stereotypical fonts) smallest character's width: "."
tmpField.htmlText = ".";
var periodCharWidth:Number = tmpField.textWidth;
tmpField.htmlText = "";
var whiteSpaceWidth:Number = tmpField.width - tf.textWidth;
var i:Number = Math.floor(whiteSpaceWidth / periodCharWidth);
while (i-- > 0)
{
tmpField.appendText(".");
}
if (whiteSpaceWidth < tmpField.textWidth)
{
while (whiteSpaceWidth < tmpField.textWidth)
{
tmpField.htmlText = tmpField.text.substr(0, -1);
}
}
else if (whiteSpaceWidth > tmpField.textWidth)
{
while (tmpField.textWidth < whiteSpaceWidth)
{
tmpField.appendText(".");
}
tmpField.htmlText = tmpField.text.substr(0, -1);
}
tf.appendText(tmpField.text);
}
// restore the settings and make the text visible again...
tf.autoSize = autoSizeSetting;
//tf.visible = true;
// defer rendering if the field is on stage to avoid the flickering of the char tests...
//if(tf.stage != null) tf.stage.invalidate( );
}
}