
Texte ActionScript
Le texte en ActionScript

On utilise la classe TextField pour créer un objet texte et la classe TextFormat pour créer et formater le texte.
Création d'un TextField (champ de texte) :
Exemple de création d'un champ de texte (AS3)
Code sur la première image clé du scénario (rien dans la bibliothèque)
// Création
var easy_txt:TextField = new TextField();
// Taille
easy_txt.width = 200;
easy_txt.height = 200;
// Champ multiligne
easy_txt.multiline = true; // multiligne
easy_txt.wordWrap = true; // retour à la ligne automatique
easy_txt.autoSize = TextFieldAutoSize.LEFT; // taille automatique (la largeur s'adaptera au contenu)
easy_txt.selectable = false; // non sélectionnable
// Bordure
easy_txt.border = true;
easy_txt.borderColor = 0xFF0000;
// Arrière plan
easy_txt.background = true;
easy_txt.backgroundColor = 0xFFE4B3;
// Contenu
easy_txt.text = "Easy-Micro, un super site Web :-)";
// Format
var formatLiens:TextFormat = new TextFormat();
formatLiens.url = "http://www.easy-micro.org";
formatLiens.color = 0x0000FF;
formatLiens.underline = true;
easy_txt.setTextFormat(formatLiens, 0, 10);
// Affichage
addChild(easy_txt);
Code sur la première image clé du scénario (rien dans la bibliothèque)
// Création
var easy_txt:TextField = new TextField();
// Taille
easy_txt.width = 200;
easy_txt.height = 200;
// Champ multiligne
easy_txt.multiline = true; // multiligne
easy_txt.wordWrap = true; // retour à la ligne automatique
easy_txt.autoSize = TextFieldAutoSize.LEFT; // taille automatique (la largeur s'adaptera au contenu)
easy_txt.selectable = false; // non sélectionnable
// Bordure
easy_txt.border = true;
easy_txt.borderColor = 0xFF0000;
// Arrière plan
easy_txt.background = true;
easy_txt.backgroundColor = 0xFFE4B3;
// Contenu
easy_txt.text = "Easy-Micro, un super site Web :-)";
// Format
var formatLiens:TextFormat = new TextFormat();
formatLiens.url = "http://www.easy-micro.org";
formatLiens.color = 0x0000FF;
formatLiens.underline = true;
easy_txt.setTextFormat(formatLiens, 0, 10);
// Affichage
addChild(easy_txt);
Note: La méthode setTextFormat, qui permet de spécifier sur quoi doit s'appliquer le formatage, attend 2 paramètres facultatif : l'index du premier caractère devant être formaté et l'index du premier caractère ne devant pas être formaté.
Note : Pour un curseur sous la forme d'une petite main sur les liens, saisir cette ligne en haut du code :
this.buttonMode = true ;
Exemple simple:
// Contenu
easy_txt.text = "Easy-Micro, un super site Web :-)";
// Format pour tous le texte
var monformat:TextFormat = new TextFormat();
monformat.font = "Arial"; monformat.bold = true;
monformat.color = 0xFFFFFF;
monformat.size = 14;
easy_txt.setTextFormat(monformat);
// Format d'un lien (doit être saisie après le format du texte)
var formatduLien:TextFormat = new TextFormat();
formatduLien.url = "http://www.easy-micro.org";
formatduLien.color = 0x0000FF;
formatduLien.underline = true;
easy_txt.setTextFormat(formatduLien, 0, 10);
// Affichage
addChild(easy_txt);
easy_txt.text = "Easy-Micro, un super site Web :-)";
// Format pour tous le texte
var monformat:TextFormat = new TextFormat();
monformat.font = "Arial"; monformat.bold = true;
monformat.color = 0xFFFFFF;
monformat.size = 14;
easy_txt.setTextFormat(monformat);
// Format d'un lien (doit être saisie après le format du texte)
var formatduLien:TextFormat = new TextFormat();
formatduLien.url = "http://www.easy-micro.org";
formatduLien.color = 0x0000FF;
formatduLien.underline = true;
easy_txt.setTextFormat(formatduLien, 0, 10);
// Affichage
addChild(easy_txt);
Note: pour déclarer le type de la zone de texte (dynamique, saisie ou statique) :
monchampdetexte.type = TextFieldType.DYNAMIC;

La classe String permet de manipuler des chaînes de texte.
◊ toUpperCase() et toLowerCase() : Ces deux méthodes renvoient une copie d'une chaîne, dont tous les caractères minuscules ont été convertis en majuscules (et inversement). Exemple :
Exemple de passage en majuscule avec toUperCase() (AS3)
function toutenmajuscule(evt:MouseEvent):void {
saisie_txt.text = saisie_txt.text.toUpperCase();
}
monbouton.addEventListener(MouseEvent.CLICK, toutenmajuscule);
function toutenmajuscule(evt:MouseEvent):void {
saisie_txt.text = saisie_txt.text.toUpperCase();
}
monbouton.addEventListener(MouseEvent.CLICK, toutenmajuscule);
Plus d'infos sur Bases-as3.fr