+ Reply to Thread
Results 1 to 1 of 1

Applicare un TextFormat ad un campo di testo

This is a discussion on Applicare un TextFormat ad un campo di testo within the Articoli e tutorials forums, part of the Flash Italiano category; Ciao a tutti ! Oggi ho deciso di trattare l' argomento TextFormat in quanto ho ricevuto diverse richieste di aiuto ...

  1. #1
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,762
    Rep Power
    11

    Applicare un TextFormat ad un campo di testo

    Ciao a tutti !
    Oggi ho deciso di trattare l' argomento TextFormat in quanto ho ricevuto diverse richieste di aiuto riguardanti come si applica una determinata font ad un campo di testo con Actionscript 3.0 .
    Intanto partirei col dire che l' iter č questo:

    1. - creare la font necessaria ed inserirla in un Array

    2. - creare un campo di testo

    3. - creare una istanza di TextFormat

    4. - assegnare il TextFormat al campo di testo

    5. - assegnare il testo al campo di test


    detto questo, bando ai blah blah ed entriamo in un esempio concreto...

    Creo un FLA che salvo con nome ' main.fla '.
    Al suo interno creo una nuova font in libreria come ho fatto vedere in questo tutorial.
    Creo la Document Class, un file AS che salvo con nome ' Main.as ', implementata in questo modo:
    Code:
    package
    {
    	import flash.display.MovieClip;
    	import flash.text.Font;
    	import flash.text.TextField;
    	import flash.text.TextFormat;
    	
    	public class Main extends MovieClip
    	{
    		private var field_txt:TextField;
    		private var format:TextFormat;
    		
    		private var fonts_array:Array;
    		
    		public function Main()
    		{
    			init();
    		}
    		
    		private function init():void
    		{
    			Font.registerFont(Monaco);
    			
    			fonts_array=Font.enumerateFonts(false);
    			
    			createField();
    			createFormat();
    			setFormat();
    			setText();
    		}
    		
    		private function createField():void
    		{
    			field_txt=new TextField();
    			addChild(field_txt);
    			field_txt.x=20;
    			field_txt.y=20;
    			field_txt.selectable=false;
    			field_txt.embedFonts=true;
    			field_txt.multiline=true;
    			field_txt.width=300;
    			field_txt.wordWrap=true;
    		}
    		
    		private function createFormat():void
    		{
    			format=new TextFormat();
    			var font:Font=fonts_array[0];
    			format.font=font.fontName;
    			format.color=0x333333;
    			format.size=14;
    		}
    		
    		private function setFormat():void
    		{
    			field_txt.defaultTextFormat=format;
    		}
    		
    		private function setText():void
    		{
    			field_txt.text="Lorem Ipsum is simply dummy text of the printing and typesetting industry"+
    			"Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,"+
    			" when an unknown printer took a galley of type and scrambled it to make a type specimen book."+
    			"It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."+
    			"It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, "+
    			"and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
    			field_txt.height=field_txt.textHeight+10;
    		}
    	}
    }
    ottenendo il seguente risultato:






    Analizziamo il codice:

    Proprietą

    una istanza di TextField
    private var field_txt:TextField;
    un Array
    private var fonts_array:Array;

    poi seguendo lo schemino che ho precisato all' inizio,

    1- creare la font necesaria ed inserirla in un Array
    Font.registerFont(Monaco);
    fonts_array=Font.enumerateFonts(false);

    2 - creare un campo di testo
    chiamo il metodo createField e:
    creo il campo
    field_txt=new TextField();
    addChild(field_txt);
    lo posiziono
    field_txt.x=20;
    field_txt.y=20;
    gli dico di non essere selezionabile dal mouse
    field_txt.selectable=false;
    imposto la proprietą embedFonts a true
    field_txt.embedFonts=true;
    dico che deve essere multi linea
    field_txt.multiline=true;
    imposto una larghezza del campo di testo
    field_txt.width=300;
    imposto la proprietą wordWrap a true
    field_txt.wordWrap=true;

    3 - creare una istanza di TextFormat
    chiamo il metodo createFormat
    creo un nuovo TextFormat
    format=new TextFormat();
    assegno la font a format
    var font:Font=fonts_array[0];
    format.font=font.fontName;
    assegno un colore a format
    format.color=0x333333;
    assegno una dimensione a format
    format.size=14;

    4- assegnare il TextFormat al campo di testo
    chiamo il metodo setFormat che assegna il textFormat al campo di testo in questo modo
    field_txt.defaultTextFormat=format;

    5 - assegnare il testo al campo di test
    chiamo il metodo setText
    assegno il testo
    field_txt.text="......";
    gli assegno una lunghezza massima al campo di testo
    field_txt.height=field_txt.textHeight+10;

    Allego i files sorgente:
    Attached Files

+ Reply to Thread

Similar Threads

  1. caratteri accentati campo testo dinamico
    By gianlucafg in forum Actionscript 3.0 base
    Replies: 5
    Last Post: 20-10-09, 13:12
  2. HELP-Campo di testo da xml da scrollare
    By susannac in forum Actionscript 3.0 base
    Replies: 9
    Last Post: 30-07-09, 08:36
  3. Replies: 1
    Last Post: 25-02-09, 12:07
  4. voglio campo di testo con collegamento url
    By simscu in forum Flash Italiano
    Replies: 1
    Last Post: 01-04-08, 08:49
  5. Altezza campo di testo dinamico
    By ulisse46 in forum Actionscript 3.0 base
    Replies: 3
    Last Post: 11-03-08, 18:23

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts