Flex: Show HTML text inside an Alert

As i just wanted to insert some bold text into an Alert box, I wondered why the Flex Alert doesn’t support HTML Text. So i wrote a Custom Alert class which just overwrites the regular Alert.show(). Through a trick, it allows to use HTML tags inside the text.

package tools
{
	import flash.display.Sprite;

	import mx.controls.Alert;
	import mx.core.IFlexModuleFactory;
	import mx.core.mx_internal;

	/**
	 * Alert-Box which makes using HTML text possible
	 */
	public class HTMLAlert extends Alert
	{
		/**
		 * Given text is interpreted as HTML text,
		 * Example: "bold text" is rendered as bold text.
		 */
		public static function show(text:String = "", title:String = "",
						flags:uint = 0x4,
						parent:Sprite = null,
						closeHandler:Function = null,
						iconClass:Class = null,
						defaultButtonFlag:uint = 0x4,
						moduleFactory:IFlexModuleFactory = null):Alert
		{
			var alert:Alert = Alert.show(text, title, flags, parent,
				closeHandler, iconClass, defaultButtonFlag, moduleFactory);
			alert.mx_internal::alertForm.mx_internal::textField.htmlText = text;
			return alert;
		}
	}
}

Just use it by calling it like the regular Alert-class:

HTMLAlert.show("A bold Alert text", "HTMLAlert", Alert.OK);

Which then looks like this:

This entry was posted in General and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

4 Comments

  1. Posted 15. January 2011 at 18:56 | Permalink

    Hey,
    happy new year,
    how comes that u have to use Flex? ;)

    I hope we will see us soon,
    karfau

  2. ingo
    Posted 15. January 2011 at 19:16 | Permalink

    Thx for ur comment ;) Happy new year to you, too!
    I’m currently helping a student getting a little Flex-project running – in my spare time.

    Yeah i think we’ll meet again after AS finished his thesis.

  3. Valdemar
    Posted 15. February 2011 at 10:14 | Permalink

    Many thanks for this class. Very usefull!

  4. Posted 13. December 2011 at 10:28 | Permalink

    Brilliant… Thanks for sharing. I was trying to do the same.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>