1
8
9 import TextField.StyleSheet;
10
11
14 class com.sekati.utils.TextUtils {
15
16
33 public static function create(scope:MovieClip, instanceName:String, x:Number, y:Number, w:Number, h:Number, props:Object, fprops:Object, css:String):TextField {
34 var t:TextField = scope.createTextField( instanceName, scope.getNextHighestDepth( ), x, y, w, h );
35 for (var i in props) {
36 t[i] = props[i];
37 }
38 if (fprops) {
39 TextUtils.format( t, fprops );
40 }
41 if (css) {
42 TextUtils.style( t, css );
43 }
44 if (props.htmlText || props.text) {
45 t.htmlText = (props.htmlText) ? props.htmlText : props.text;
46 }
47 return t;
48 }
49
50
60 public static function format(tf:TextField, props:Object, css:String):Void {
61 var str:String = (tf.html) ? tf.htmlText : tf.text;
62 var f:TextFormat = new TextFormat( );
63 for (var i in props) {
64 f[i] = props[i];
65 }
66 tf.setNewTextFormat( f );
67 if(tf.html) {
68 tf.htmlText = str;
69 } else {
70 tf.text = str;
71 }
72 }
73
74
85 public static function style(tf:TextField, css:String):Void {
86 var str:String = tf.htmlText;
87 if (css.substr( css.length - 4, 4 ).toLowerCase( ) != ".css") {
88 var _styles:TextField.StyleSheet = new StyleSheet( );
89 if (_styles.parseCSS( css )) {
90 tf.styleSheet = _styles;
91 tf.htmlText = str;
92 }
93 } else {
94 var _styleSheet:TextField.StyleSheet = new StyleSheet( );
95 _styleSheet.onLoad = function (success:Boolean):Void {
96 if (success) {
97 tf.styleSheet = _styleSheet;
98 tf.htmlText = str;
99 }
100 };
101 _styleSheet.load( css );
102 }
103 }
104
105
114 public static function focusToggle(tf:TextField):Void {
115 trace( "focus toggle called" );
116 if (tf.defval.length) {
117 if (tf.text == tf.defval) {
118 tf.text = "";
119 } else if (tf.text == "") {
120 tf.text = tf.defval;
121 }
122 } else if (tf.text.length) {
123 tf.defval = tf.text;
124 tf.text = "";
125 }
126 }
127
128
138 public static function caret(tf:TextField, index:Number):Void {
139 Selection.setFocus( tf );
140 Selection.setSelection( index, index );
141 }
142
143
153 public static function select(tf:TextField, sIndex:Number, eIndex:Number):Void {
154 Selection.setFocus( tf );
155 Selection.setSelection( sIndex, eIndex );
156 }
157
158
170 public static function antiAlias(tf:TextField, thick:Number, sharp:Number, gridFitType:Number, isAdvanced:Boolean):Void {
171 var fits:Array = [ "none", "pixel", "subpixel" ];
172 tf.antiAliasType = (!isAdvanced) ? "normal" : "advanced";
173 tf.thickness = (thick) ? thick : 0;
174 tf.sharpness = (sharp) ? sharp : 0;
175 tf.gridFitType = (gridFitType && gridFitType <= 2) ? fits[gridFitType] : fits[1];
176 }
177
178
185 public static function excerpt(tf:TextField, n:Number, ellipseLine:Boolean):Void {
186 if (tf.bottomScroll > n) {
187 var len:Number = tf.text.length;
188 for (var i:Number = 0; i < len ; i++) {
189 tf.scroll = tf.maxscroll;
190 if (tf.bottomScroll > n) {
191 tf.text = tf.text.slice( 0, -1 );
192 } else {
193 var e:Number = (ellipseLine) ? -3 : tf.text.lastIndexOf( " " );
194 tf.text = tf.text.slice( 0, e );
195 tf.text += "...";
196 break;
197 }
198 }
199 }
200 }
201
202
210 public static function chop(tf:TextField, cutW:Number, ellipse:Boolean, longEllipse:Boolean):Void {
211 cutW = (!longEllipse) ? cutW - 3 : cutW -= 10;
212 if (tf.textWidth <= cutW && !longEllipse) {
213 return;
214 }
215 tf._visible = false;
216 var str:String = tf.text;
217
218 tf.text = "W";
219 var ww:Number = tf.textWidth;
220 tf.text = "A";
221 var aw:Number = tf.textWidth;
222 var ptr:Number = Math.floor( cutW / ww );
223 var origLen:Number = str.length;
224 var checkS:String = str.substr( 0, ptr );
225 tf.text = checkS;
226 var cnt:Number;
227 while (tf.textWidth < cutW) {
228 cnt = Math.floor( (cutW - tf.textWidth) / aw );
229 if (cnt == 0) {
230 break;
231 }
232 ptr += cnt;
233 ptr = (ptr > origLen) ? origLen : ptr;
234 checkS = str.substr( 0, ptr );
235 tf.text = checkS;
236 if (origLen == ptr) {
237 break;
238 }
239 }
240 tf.text = (ellipse) ? tf.text + "..." : tf.text;
241 if (tf.textWidth > cutW) {
242 while (tf.textWidth > cutW) {
243 checkS = checkS.substr( 0, -1 );
244 tf.text = ellipse ? checkS + "..." : checkS;
245 }
246 }
247 if (longEllipse) {
248 tf._parent.tf2.text = ".";
249 var periodW:Number = tf._parent.tf2.textWidth;
250 tf._parent.tf2.text = "";
251 var whiteSp:Number = cutW - tf.textWidth + 8;
252 var i:Number = Math.floor( whiteSp / periodW );
253 while (i-- > 0) {
254 tf._parent.tf2.text += ".";
255 }
256 if (whiteSp < tf._parent.tf2.textWidth) {
257 while (whiteSp < tf._parent.tf2.textWidth) {
258 tf._parent.tf2.text = tf._parent.tf2.text.substr( 0, -1 );
259 }
260 } else if (whiteSp > tf._parent.tf2.textWidth) {
261 while (tf._parent.tf2.textWidth < whiteSp) {
262 tf._parent.tf2.text += ".";
263 }
264 tf._parent.tf2.text = tf._parent.tf2.text.substr( 0, -1 );
265 }
266 }
267 tf._visible = true;
268 }
269
270
277 public static function simpleChop(tf:TextField, maxW:Number, ellipse:Boolean):Void {
278 var dots:String = "", oht:Boolean = tf.html, cn:String = "$__chop__" + tf._name;
279 if (!maxW) {
280 maxW = tf._width;
281 }
282 if (ellipse) {
283 dots = "...";
284 }
285 if (tf._parent[cn]) {
286 tf._parent[cn].removeTextField( );
287 }
288 var chopField:TextField = tf._parent.createTextField( cn, tf._parent.getNextHighestDepth( ), -1000, -1000, 1000, 1000 );
289 tf.html = true,
290 chopField.html = true,
291 chopField.type = tf.type,
292 chopField._visible = false;
293 chopField.setTextFormat( tf.getTextFormat( ) );
294 chopField.htmlText = "W";
295
296 maxW -= chopField.textWidth;
297
298 chopField.htmlText = tf.htmlText;
299 var tw:Number = chopField.textWidth;
300 if (tw > maxW) {
301 var t:String = tf.htmlText;
302 while (tw > maxW) {
303 t = t.substr( 0, t.length - 1 );
304 chopField.htmlText = t + dots;
305 tw = chopField.textWidth;
306 }
307 tf.htmlText = t + dots;
308 }
309 tf.html = oht;
310 chopField.removeTextField( );
311 }
312
313 private function TextUtils() {
314 }
315 }