Flash / Flex / ActionScript/TextField/text format — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 09:19, 26 мая 2010

Apply alignment formatting to the entire span of characters in the second paragraph.

 
package{
  import flash.display.Sprite;
  import flash.text.*;
  public class Main extends Sprite{
    public function Main(){
        var t:TextField = new TextField(  );
        t.width = 100;
        t.border = true;
        t.wordWrap = true;
        t.text = "This is paragraph one.\nThis is paragraph two.";
        var alignFormat:TextFormat = new TextFormat(  );
        alignFormat.align = TextFormatAlign.CENTER;
        
        var firstParagraphStart:int = 0;
        var firstParagraphEnd:int = t.getParagraphLength(firstParagraphStart)-1;
        var secondParagraphStart:int = firstParagraphEnd+1;
        var secondParagraphEnd:int = secondParagraphStart+ t.getParagraphLength(secondParagraphStart)-1;
        
        t.setTextFormat(alignFormat, secondParagraphStart, secondParagraphEnd);
        addChild(t);
    }
  }
}



Change Text format

 
package {
  import flash.display.Sprite;
  import flash.text.TextField;
  import flash.text.TextFormat;
  
  public class Main extends Sprite {
    public function Main(  ) {
      var field:TextField = new TextField(  );
      var formatter:TextFormat = new TextFormat(  );
      formatter.bold = true;       // Bold the text
      formatter.color = 0xFFFF00;  // Make the text yellow
      formatter.blockIndent = 5;   // Adjust the margin by 5 points
    
      field.setTextFormat(formatter);
      field.text = "this is sample text";
      field.setTextFormat(formatter);    // Formatting applied
      field.text = "this is new text";   // No formatting applied
      field.setTextFormat(formatter);    // Formatting reapplied
      field.text += "appended text";     // Formatting removed
    
      addChild(field);
    }
  }
}



Default formatting for text fields

 
package{
  import flash.display.Sprite;
  import flash.text.*;  
  public class Main extends Sprite{
    public function Main(){
        var t:TextField = new TextField(  );
        t.width = 400;
        t.text = "This is bold text.";
        var boldFormat:TextFormat = new TextFormat(  );
        boldFormat.bold = true;
        t.setTextFormat(boldFormat);
        
        t.appendText(" This is bold too.");
        addChild(t);
    }
  }
}



Embedded font warning: Bold and italic require separate fonts

 
package{
  import flash.display.Sprite;
  import flash.text.*;  
  public class Main extends Sprite{
    public function Main(){
        var t:TextField = new TextField(  );
        t.text = "hello world";
        
        var fontFormat:TextFormat = new TextFormat(  );
        fontFormat.font = "Courier New";
        var boldFormat:TextFormat = new TextFormat(  );
        boldFormat.bold = true;
        
        t.setTextFormat(fontFormat, 0, 11);
        t.setTextFormat(boldFormat, 6, 11);
        addChild(t);
    }
  }
}



Formatting a Portion of Existing Text

 
package {
  import flash.display.Sprite;
  import flash.text.TextField;
  public class Main extends Sprite {
    public function Main(  ) {
      var field:TextField = new TextField(  );
      var formatter:flash.text.TextFormat = new flash.text.TextFormat(  );
      formatter.color = 0x0000FF;      // Make the text blue
      field.text="12345678900987654321asdfasdf";   
      field.setTextFormat(formatter,0,10);                               
      field.setTextFormat( formatter,10,20); 
      addChild(field);
    }
  }
}



Formatting User-Input Text

 
package {
  import flash.display.Sprite;
  import flash.text.TextField;
  public class Main extends Sprite {
    public function Main(  ) {
      var field:TextField = new TextField(  );
    
      var formatter:flash.text.TextFormat = new flash.text.TextFormat(  );
      formatter.color = 0x0000FF;      // Make the text blue
      field.defaultTextFormat = formatter;
      field.text="text";
      addChild(field);
    }
  }
}



If a specific format (e.g., font, bold, or italic) is not the same for all characters in a specified span, the corresponding variable of the TextFormat object for that span will be null

 
package{
  import flash.display.Sprite;
  import flash.text.*;  
  public class Main extends Sprite{
    public function Main(){
        var t:TextField = new TextField(  );
        t.width = 100;
        t.border = true;
        t.wordWrap = true;
        t.text = "What time is it?";
        
        var arialFormat:TextFormat = new TextFormat(  );
        arialFormat.font = "Arial";
        
        t.setTextFormat(arialFormat, 0, 4);
        
        var allCharsFormat:TextFormat = t.getTextFormat(  );
        
        trace(allCharsFormat.bold); // Displays: false
        addChild(t);
        
    }
  }
}



Retrieving formatting information for a span of characters

 
package{
  import flash.display.Sprite;
  import flash.text.*;  
  public class Main extends Sprite{
    public function Main(){
        var t:TextField = new TextField(  );
        t.width = 100;
        t.border = true;
        t.wordWrap = true;
        t.text = "What time is it?";
        
        var arialFormat:TextFormat = new TextFormat(  );
        arialFormat.font = "Arial";
        
        t.setTextFormat(arialFormat, 0, 4);
        
        var firstCharFormat:TextFormat = t.getTextFormat(0);
        
        trace(firstCharFormat.font); // Displays: Arial
        addChild(t);
    }
  }
}



Set text format by searching the substring in textfield

 
package{
  import flash.display.Sprite;
  import flash.text.*;
  public class Main extends Sprite{
    public function Main(){
        var t:TextField = new TextField(  );
        t.width = 400;
        t.text = "This is bold text.";
        var regularFormat:TextFormat = new TextFormat(  );
        regularFormat.bold = false;
        t.setTextFormat(regularFormat,
                        t.text.indexOf("This isn"t bold."),
                        t.length);
        
        addChild(t);
    }
  }
}



setTextFormat( ) does not apply to future text assignments

 

package{
  import flash.display.Sprite;
  import flash.text.*;  
  public class Main extends Sprite{
    public function Main(){
        var t:TextField = new TextField(  );
        t.autoSize = TextFieldAutoSize.LEFT;
        
        var format:TextFormat = new TextFormat(  );
        format.font = "Arial";
        format.size = 20;
        format.bold = true;
        
        t.setTextFormat(format);
        
        t.text = "ActionScript is fun!";
        addChild(t);
    }
  }
}



Setting a Text Field"s Font

 
package {
  import flash.display.Sprite;
  import flash.text.TextField;
  public class Main extends Sprite {
    public function Main(  ) {
        var field:TextField = new TextField(  );
        var formatter:flash.text.TextFormat = new flash.text.TextFormat(  );
        formatter.color = 0x0000FF;      // Make the text blue
        field.htmlText = "<font face="Arial">Formatted text</font>";
        formatter.font = "Arial";
        formatter.font = "Arial, Verdana, Helvetica";
        addChild(field);
    }
  }
}



The format for an empty TextField object is specified by assigning a TextFormat object to the TextField object"s defaultTextFormat variable

 

package{
  import flash.display.Sprite;
  import flash.text.*;
  public class Main extends Sprite{
    public function Main(){
        var t:TextField = new TextField(  );
        t.width = 300;
        
        var defaultFormat:TextFormat = new TextFormat(  );
        defaultFormat.size  = 20;
        defaultFormat.color = 0xFF0000;
        defaultFormat.font  = "Verdana";
        
        t.defaultTextFormat = defaultFormat;
        
        t.text = "This is 20 pt red Verdana";
        addChild(t);
    }
  }
}



To set the alignment for a range of paragraphs, we apply the format using beginIndex and endIndex arguments that include the desired paragraphs:

 
package{
  import flash.display.Sprite;
  import flash.text.*;  
  public class Main extends Sprite{
    public function Main(){
        var t:TextField = new TextField(  );
        t.width = 300;
        t.border = true;
        
        t.text = "This is paragraph one.\nThis is paragraph two.";
        
        var alignFormat:TextFormat = new TextFormat(  );
        alignFormat.align = TextFormatAlign.CENTER;
        
        t.setTextFormat(alignFormat, 0, 24);
        addChild(t);
    }
  }
}



To set the alignment for the first paragraph in the text field only, we apply the format to the first character in the first paragraph, which resides at index 0:

 
package{
  import flash.display.Sprite;
  import flash.text.*;  
  public class Main extends Sprite{
    public function Main(){
        var t:TextField = new TextField(  );
        t.width = 300;
        t.border = true;
        
        t.text = "This is paragraph one.\nThis is paragraph two.";
        
        var alignFormat:TextFormat = new TextFormat(  );
        alignFormat.align = TextFormatAlign.CENTER;
        
        t.setTextFormat(alignFormat, 0);
        addChild(t);
    }
  }
}



To set the alignment for the second paragraph only, we apply the format to the first character in the second paragraph, which resides at index 23:

 

package{
  import flash.display.Sprite;
  import flash.text.*;  
  public class Main extends Sprite{
    public function Main(){
        var t:TextField = new TextField(  );
        t.width = 300;
        t.border = true;
        
        t.text = "This is paragraph one.\nThis is paragraph two.";
        
        var alignFormat:TextFormat = new TextFormat(  );
        alignFormat.align = TextFormatAlign.CENTER;
        
        t.setTextFormat(alignFormat, 23);
        addChild(t);
    }
  }
}



Two formats

 

package{
  import flash.display.Sprite;
  import flash.text.*;
  public class Main extends Sprite{
    public function Main(){
        var t:TextField = new TextField(  );
        t.text = "ActionScript is fun!";
        t.autoSize = TextFieldAutoSize.LEFT;
        
        var fontFormat:TextFormat = new TextFormat(  );
        fontFormat.font = "Arial";
        fontFormat.size = 20;
        
        var boldFormat:TextFormat = new TextFormat(  );
        boldFormat.bold = true;
        
        t.setTextFormat(fontFormat);
        
        t.setTextFormat(boldFormat, 16, 19);
        addChild(t);
    }
  }
}



Use the TextFormat object"s color and underline properties

 
package {
  import flash.display.Sprite;
  import flash.text.TextField;
  public class Main extends Sprite {
    public function Main(  ) {
      var field:TextField = new TextField(  );
    
        field.text = "Website";
        var formatter:flash.text.TextFormat = new flash.text.TextFormat(  );
        formatter.color = 0x0000FF;
        formatter.underline = true;
        formatter.url = "http://www.wbex.ru/";
        field.setTextFormat(formatter);
        addChild(field);
    }
  }
}