View comments | RSS feed

Embedding images, SWF files, and movie clips in text fields

In Flash Player 7 and later, you can use the <img> tag to embed JPEG files, SWF files, and movie clips inside dynamic and input text fields, and TextArea component instances. (For a full list of attributes for the <img> tag, see Image tag (<img>).)

Flash displays media embedded in a text field at full size. To specify the dimensions of the media you are embedding, use the <img> tags's height and width attributes. (See Specifying height and width values.)

In general, an image embedded in a text field appears on the line following the <img> tag. However, when the <img> tag is the first character in the text field, the image appears on the first line of the text field.

Embedding SWF and JPEG files

To embed a JPEG or SWF file in a text field, specify the absolute or relative path to the JPEG or SWF file in the <img> tag's src attribute. For example, the following code inserts a JPEG file that's located in the same directory as the SWF file:

textField_txt.htmlText = "<p>Here's a picture from my last vacation:<img src='beach.jpg'>";

Embedding movie clip symbols

To embed a movie clip symbol in a text field, you specify the symbol's linkage identifier for the <img> tag's src attribute. (For information on defining a linkage identifier, see Attaching a movie clip symbol to the Stage.)

For example, the following code inserts a movie clip symbol with the linkage identifier symbol_ID into a dynamic text field with the instance name textField_txt.

textField_txt.htmlText = "<p>Here's a movie clip symbol:<img src='symbol_ID'>";

In order for an embedded movie clip to display properly and completely, the registration point for its symbol should be at point (0,0).

Specifying height and width values

If you specify width and height attributes for an <img> tag, space is reserved in the text field for the JPEG file, SWF file, or movie clip. After a JPEG or SWF file has downloaded completely, it appears in the reserved space. Flash scales the media up or down, according to the height and width values. You must enter both height and width attributes to scale the image.

If you don't specify height and width values, no space is reserved for the embedded media. After a JPEG or SWF file has downloaded completely, Flash inserts it into the text field at full size and rebreaks text around it.

Note: If you are dynamically loading your images into a text field containing text, it is good practice to specify the width and height of the original image so the text properly wraps around the space you reserve for your image.

Controlling embedded media with ActionScript

Flash Player creates a new movie clip for each <img> tag and embeds that movie clip within the TextField object. The <img> tag's id attribute lets you assign an instance name to the movie clip that is created. This lets you control that movie clip with ActionScript.

The movie clip created by Flash Player is added as a child movie clip to the text field that contains the image.

For example, the following code embeds a SWF file named animation.swf in the text field named textField_txt on level 0 and assigns the instance name animation_mc to the movie clip that contains the SWF file:

_level0.textField_txt.htmlText = "Here's an interesting animation: <img src='animation.swf' id='animation_mc'>";

In this case, the fully qualified path to the newly created movie clip is _level0.textField_txt.animation_mc. For example, you could place the following code after the previous code (on the same Timeline as textField_txt) for a button called stop_btn that stops the playhead of the embedded SWF file:

stop_btn.onRelease = function() {
   _level0.textField_txt.animation_mc.stop();
};

Making hypertext links out of embedded media

To make a hypertext link out of an embedded JPEG file, SWF file, or movie clip, enclose the <img> tag in an <a> tag:

textField.htmlText = "Click the image to return home<a href='home.htm'><img src='home.jpg'></a>";

When the mouse pointer is over an image, SWF file, or movie clip that is enclosed by <a> tags, the pointer turns into a "pointing hand" icon, the same as it does with standard hypertext links. Interactivity, such as mouse clicks and keypresses, does not register in SWF files and movie clips that are enclosed by <a> tags.


Comments


garbylong said on Oct 23, 2004 at 1:55 PM :
Of course, I had the same problem, with an image in a dynamically loaded text field filled with LoadVars. The fix I found was this. After the src tag, put enough returns in the text to create enough space for the image, then put a period at the end of the returns. Having this bit of text forces the space made by the returns to appear. (Of course, it's obnoxious to have that period down there but it's _almost_ unnoticable.) Another problem I had was that with the image embedded the text did not appear until going to another frame and back again which predictably caused a crash in Flash or even a browser. The solution I found to this was to enclose the src tag, returns and period with paragraph tags. That solved the crash (which indicated I believe a memory issue),

My code looked like this:

<p>
<img src="fin_image" id="fin_image" width="85" height="103" hspace="130" vspace="20">

[a number of returns here]

.</p>

See if this works.
cronodragon said on May 31, 2005 at 10:20 PM :
I'm working on a phpBB-like forum in Flash, and wanted to have those yellow smiles. I tried to place an image in the middle of some text, and Flash pushes the image into the next line, showing a blank space in between. Is there a way to place images between text... or maybe in future versions? Or is it possible to extend the TextArea component to support those images and other HTML tags? Have someone made it already?

Regards!
RMAX said on Jun 8, 2005 at 6:11 AM :
It doesnt seem possible to insert a picture as wide as the textfield you are using. When you try this the text after the image will dissapear outside the textbox of below the image.

i used the following order:

textfield containing text + image + text
movieclip

the movieclip should be positioned right under the textfield which has a vriable height. However in the setting above the image will overlap the second text bit. and using:

textfield containg text + text + image
movieclip

will position the movieclip to high, underneat the text overlapping the image. Putting a &nbsp; after the image would fix this, but its still impossible to insert a wide image between text paragraphs.
shubhojoy said on Aug 20, 2005 at 11:51 PM :
I noticed two things while trying to load external XML content into dynamic text fields and display external images using the supported img tag:

1. Generally, if you want to next html tags into XML tags, use special chars &lt; and &gt; instead of "<" or ">". It works fine when the XML node value is loaded in a dnamic text field

2. Secondly, you can display jpeg images with img src tag as long as it is not progressive jpeg! Had me baffled for hours until I found the vital piece of info on a tute [shouts to Neil Webb (www.nwebb.co.uk)]...

enjoy!
johnnystorm0 said on Aug 22, 2005 at 1:20 PM :
You can also use:

<description><![CDATA[HTML <b>formatted</b> <i>text</i> ]]></
description>

But when doing bolding and italics its best to use style sheets to control
the look and feel:

<span class="bold">This is bold text</span> instead of <b></b>
salimsdesign said on Sep 6, 2005 at 3:25 AM :
It is really very helpful. But I am facing some problem.

When I write the code in the first frame of my movie, it is working fine.

Here is the code.

_root.main.dt.htmlText="<img src='image.jpg'>"

But when I want to write the same script to a button, it is not working. What is the reason for that?
swingheim said on Sep 30, 2005 at 10:14 AM :
Does anyone else have problems with images being resized at random?

I specify width="100" and height="100", and the image comes out being something closer to 87x94.
No screen name said on Oct 6, 2005 at 9:51 AM :
It appears that embedded images take longer to render than text, which makes sense. However, there does not seem to be a way to determine when an embedded image has finished rendering. This causes problems when trying to print a text field with embedded images on the fly. For example, if we start a print job, create a dynamic text field based on the page dimensions, load in some html text with an embedded image, and send it to the printer, the embedded image does not print. Instead we get white space where the image should be. Of course, we can print an existing text box fine, but this does not allow for us to size the page based on the printer settings. Is there are solution for this other than creating a delay function and waiting a predetermined amount of time before sending the print job?

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/mx2004/main_7_2/00001046.html