Recipe 9.25. Adding a Hyperlink to Text


Problem

You want to hyperlink some of the text displayed in a text field.

Solution

Use HTML <a href> tags within the object's htmlText property. Alternatively, use a TextFormat object with a value assigned to the url property.

Discussion

Both solutions to this problem require that you set the text field's html property to TRue:

field.html = true;

If you want to use HTML to add a hyperlink, add an <a href> tag to the text field's htmlText property, as follows:

field.htmlText = "<a href='http://www.rightactionscript.com'>Website</a>";

You can add a target window into which to open the link by adding a target attribute to the <a href> HTML tag. For example:

field.htmlText = "<a href='http://www.rightactionscript.com'  target='blank'>Website</a>";

When text is hyperlinked in Flash, the mouse cursor changes to a hand when it is over the linked text. Flash does not inherently provide any indication that the text is linked, unlike most HTML browsers (which use an underline and color change). For this reason, it is helpful to add HTML markup that underlines and colors the linked text:

var htmlLink:String = "<font color='#0000FF'><u>"; htmlLink += "<a href='http://www.rightactionscript.com'>Website</a>"; htmlLink += "</u></font>"; field.htmlText = htmlLink;

You can accomplish the same tasks without HTML by using a TextFormat object. The TextFormat class includes a url property for just this purpose. Assigning the URL to the url property links the formatted text; for example:

field.text = "Website"; var formatter:TextFormat = new TextFormat(  ); formatter.url = "http://www.rightactionscript.com/"; field.setTextFormat(formatter);

If you want to specify a target window into which the link opens, you can set the value of the TextFormat object's target property, as follows:

field.text = "Website"; var formatter:TextFormat = new TextFormat(  ); formatter.url = "http://www.rightactionscript.com/"; formatter.target = "_blank"; field.setTextFormat(formatter);

As with the HTML technique, when using a TextFormat object to create a hyperlink, Flash does not offer any indication as to the link's presence other than the hand cursor when it is moused over. You can add color and/or an underline to the linked text to provide the user with the indication that it is a link. You should use the TextFormat object's color and underline properties for this purpose:

field.text = "Website"; var formatter:TextFormat = new TextFormat(  ); formatter.color = 0x0000FF; formatter.underline = true; formatter.url = "http://www.rightactionscript.com/"; field.setTextFormat(formatter);

You can use either of the techniques in this recipe to add links that point not only to http and https protocols, as shown in the examples, but also to link to other protocols. For example, you can use the same techniques to open a new email message:

field.text = "email"; var formatter:TextFormat = new TextFormat(  ); formatter.color = 0x0000FF; formatter.underline = true; formatter.url = "mailto:joey@person13.com"; field.setTextFormat(formatter);

Be aware, however, that many other types of links (such as mailto links) only work when the movie is played in a web browser in which a default client for the protocol has been defined.

Using CSS, you can apply advanced formatting to <a href> tags by using the a:link, a:active, and a:hover styles, as shown in the following example:

var css:StyleSheet = new StyleSheet(  ); css.parseCSS("a {color: #0000FF;} a:hover {text-decoration: underline;}"); field.styleSheet = css; field.html = true; field.htmlText = "<a href='http://www.rightactionscript.com'>Website</a>";

See Also

Recipes 9.8 and 9.15




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net