Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Apply a TextFormat to a TextField

This is a discussion on Apply a TextFormat to a TextField within the Tutorials forums, part of the Flash English category; Greetings to all! Today, I decided to talk about TextFormat as I have been asked more then once some help ...


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-11-07, 06:56
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Apply a TextFormat to a TextField

Greetings to all!
Today, I decided to talk about TextFormat as I have been asked more then once some help on how to apply a chosen font to a text field with Actionscript 3.0.

I will start by telling you the basics:

1. " create the needed font and insert it in an Array
2. " create a text field
3. " create an instance of TextFormat
4. " assign the TextFormat to the text field
5. " assign the text to the text field

Without losing any time, let us take a look at a concrete example"

I create a FLA and I save it as "main.fla".
Into which, I create a new font in library as seen in this tutorial.
I create the Document Class, an AS file saved as "Main.as", implemented the following way:
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;
		}
	}
}
to obtain the following result:






Let us analyse the code:

Properties

an instance of TextField
private var field_txt:TextField;
an Array
private var fonts_array:Array;

Following the basic steps explained above,

1 " create the needed font and insert it in an Array
Font.registerFont(Monaco);
fonts_array=Font.enumerateFonts(false);

2 " create a text field
I call the method createTextField and:
I create the field
field_txt=new TextField();
addChild(field_txt);
I position it
field_txt.x=20;
field_txt.y=20;
I declare that it is not selectable
field_txt.selectable=false;
I impost the property embedFonts to true
field_txt.embedFonts=true;
I declare it as multi line
field_txt.multiline=true;
I impost the width of the text field
field_txt.width=300;
I impost the property wordWrap to true
field_txt.wordWrap=true;

3 - create an instance of TextFormat
I call the method createFormat
I create a new textFormat
format=new TextFormat();
I assign the font to "format"
var font:Font=fonts_array[0];
format.font=font.fontName;
I assign a colour to "format"
format.color=0x333333;
I assign a size to "format"
format.size=14;

4 - assign the TextFormat to the text field
I call the method setFormat which assign the textFormat to the text field this way:
field_txt.defaultTextFormat=format;

5 - assign the text to the text field
I call the method setText
I assign the text
field_txt.text="......";
I assign a maximum width to the text field
field_txt.height=field_txt.textHeight+10;

Source files:
Attached Files
File Type: zip TextFormat.zip (27.7 KB, 27 views)

__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !

Last edited by Flep; 28-08-08 at 06:50..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Apply the BlurFilter to a MovieClip with Actionscript 3.0 - script 2 Flep Tutorials 1 25-06-08 08:31
Set focus to TextField venkatesh Flash English 1 14-06-08 10:53
Problema con textfield neeohak Actionscript 3.0 avanzato 4 05-06-08 19:07
textfield & c.. nootropic.kint Flash Italiano 1 14-11-07 18:39
Applicare un TextFormat ad un campo di testo Flep Articoli e tutorials 0 27-09-07 06:31


All times are GMT. The time now is 12:11.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap