Como sabeis, la clase textfield cuenta con la propiedad maxChars que permite limitar el número de caracteres que acepta un campo de entrada. Normalmente, es muy útil, pero ¿que pasa si no quiero que en el computo de caracteres introducidos se tengan en cuenta los espacios en blanco? Pues que no puede ser, el espacio en blanco es tambien un caracter.
Un ejemplo ilustrativo: Imaginad que en un campo de introducción, limitamos el número de caracteres a 27 (en realidad queremos letras o digitos sin contar espacios en blanco), y queremos escribir:
'A B C D E F G H I J K L M N Ñ O P Q R S T U V W X Y Z'
Utilizando la propiedad maxChars escribimos:
-
this.createTextField ("texto_txt", this.getNextHighestDepth (), 10, 10, 100, 22);
-
texto_txt.type = "input";
-
texto_txt.border = true;
-
texto_txt.maxChars = 27;
Nos quedariamos en:
'A B C D E F G H I J K L M N'
Con la clase maxchars:
-
class maxchars {
-
public function maxchars() {}
-
public static function listen(TEXTFIELD:TextField, max:Number) {
-
TEXTFIELD.onChanged = function() {
-
var numChars:Number = count(TEXTFIELD.text);
-
if (numChars> max) {
-
TEXTFIELD.text = TEXTFIELD.text.substring(0, TEXTFIELD.length - 1);
-
}
-
};
-
}
-
public static function count(STRING:String):Number {
-
return STRING.split(" ").join("").length;
-
}
-
}
Ejemplo de uso:
-
import maxchars;
-
my_format = new TextFormat ();
-
my_format.font = 'verdana';
-
my_format.size = 10;
-
texto_txt = this.createTextField ("texto_txt", this.getNextHighestDepth (), 10, 10, 400, 16);
-
texto_txt.type = "input";
-
texto_txt.border = true;
-
texto_txt.setNewTextFormat (my_format);
-
maxchars.listen (texto_txt, 27);

0 Responses to “maxchars: Clase que emula la propiedad maxChars sin contar espacios en blanco.”