-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextFieldUtils.as
74 lines (57 loc) · 1.5 KB
/
TextFieldUtils.as
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
package
{
import flash.text.*;
import org.flashdevelop.utils.FlashConnect;
/**
* ...
* @author Fernando de França
*/
public class TextFieldUtils
{
public static function elastic($tf:TextField, $wordWrap:Boolean=true):void
{
$tf.wordWrap = $wordWrap;
$tf.autoSize = 'left';
}
public static function setLetterSpacing($tf:TextField, $value:Number):void
{
var fmt:TextFormat = $tf.defaultTextFormat
fmt.letterSpacing = $value;
$tf.defaultTextFormat = fmt;
}
public static function limit(tf:TextField, $textoNovo:String, $strFinal:String='...'):String
{
var oldText:String = tf.text;
tf.text = $textoNovo;
var cond:Boolean = tf.textWidth > tf.width; // estourou?
var arrWords:Array = $textoNovo.split(' ');
// não precisa limitar
if (cond==false || arrWords.length < 2)
{
tf.text = oldText
return $textoNovo
}
// limita
tf.text = '';
var i:int = 1;
var txtTest:String
while (tf.textWidth < tf.width)
{
txtTest = arrWords.slice(0, i).join(' ') + $strFinal;
tf.text = txtTest;
i++;
}
txtTest = arrWords.slice(0, i-2).join(' ') + $strFinal;
tf.text = oldText;
return txtTest
}
public static function focus(tf:TextField):void
{
if (tf.stage) tf.stage.focus = tf;
}
public static function blur(tf:TextField):void
{
if (tf.stage) tf.stage.focus = null;
}
}
}