Adding text to a flash movie is a basic requirement. Aspose.Flash allows you to add text to an SWF file using DefineEditText class. This class can also be used to add either simple text or HTML in a Flash movie.
In this example, we’ll learn to define a font for a text object and then add simple text to a flash movie. The following steps can be used to add a DefineFont2 object and then add text using DefineEditText class:
- Create FlashContainer object and set FrameRate and FrameSize properties
- Define background color using SetBackgroundColor class
- Create a new font using DefineFont2 class
- Create a text object using DefineEditText class
- Place the text object on FlashContainer using PlaceObject2 class
- Add ShowFrame object and save the movie
The example below will show you how to create font and add a text object in the flash movie.
[C#]
//create FlashContainer object
FlashContainer flash = new FlashContainer();
// set frame rate
flash.FrameRate = 35.0f;
// set frame size
flash.FrameSize = new Rect(0, 0, 4000, 4000);
//define background color
SetBackgroundColor bg = new SetBackgroundColor(new RGB(0, 255, 255, 255));
flash.Add(bg);
//define a new font object as DefineFont2
DefineFont2 font = new DefineFont2(flash.NewIdentifier(), "Verdana");
font.Small = false;
font.Italic = false;
font.Bold = false;
font.Language = TextLanguage.Latin;
font.Leading = 0;
font.CodeTable = new ArrayList();
font.GlyphShapeTable = new ArrayList();
flash.Add(font);
//create text object using DefineEditText
DefineEditText text = new DefineEditText(flash.NewIdentifier(), new Rect(0, 0, 3000, 1000));
text.WordWrap = false;
text.Multiline = true;
text.Password = false;
text.HTML = false;
text.NoSelect = false;
text.ReadOnly = true;
text.FontId = font.FontId;
text.FontHeight = 400;
text.TextColorRGB = new RGB(0, 0, 0, 255);
text.Leading = 40;
text.InitialText = "sample text";
flash.Add(text);
//add text object to the display list using PlaceObject2
PlaceObject2 placeObject = new PlaceObject2(text.ObjectId, 1);
placeObject.CharacterType = PlaceType.New;
placeObject.MatrixTransform = new MatrixTransform(740, 548);
//add PlaceObject2 in the FlashContainer
flash.Add(placeObject);
//informs a flash player to show the content of the display list
flash.Add(new ShowFrame());
//save output flash movie
flash.Write("SimpleText.swf");
[VB.NET]
'create FlashContainer object
Dim flash As New FlashContainer()
' set frame rate
flash.FrameRate = 35.0F
' set frame size
flash.FrameSize = New Rect(0, 0, 4000, 4000)
'define background color
Dim bg As New SetBackgroundColor(New RGB(0, 255, 255, 255))
flash.Add(bg)
'define a new font object as DefineFont2
Dim font As New DefineFont2(flash.NewIdentifier(), "Verdana")
font.Small = False
font.Italic = False
font.Bold = False
font.Language = TextLanguage.Latin
font.Leading = 0
font.CodeTable = New ArrayList()
font.GlyphShapeTable = New ArrayList()
flash.Add(font)
'create text object using DefineEditText
Dim text As New DefineEditText(flash.NewIdentifier(), New Rect(0, 0, 3000, 1000))
text.WordWrap = False
text.Multiline = True
text.Password = False
text.HTML = False
text.NoSelect = False
text.[ReadOnly] = True
text.FontId = font.FontId
text.FontHeight = 400
text.TextColorRGB = New RGB(0, 0, 0, 255)
text.Leading = 40
text.InitialText = "sample text"
flash.Add(text)
'add text object to the display list using PlaceObject2
Dim placeObject As New PlaceObject2(text.ObjectId, 1)
placeObject.CharacterType = PlaceType.New
placeObject.MatrixTransform = New MatrixTransform(740, 548)
'add PlaceObject2 in the FlashContainer
flash.Add(placeObject)
'informs a flash player to show the content of the display list
flash.Add(New ShowFrame())
'save output flash movie
flash.Write("SimpleText.swf")
The output image shows you the simple text added to the flash movie with the font applied.