Superscript and Subscript in Actionscript

Unfortunately, superscripting and subscripting text in actionscript is not an included feature of TextFields. So, I’ve built a utility class to handle all your superscripting and subscripting needs. I’m just going to give a general overview here but provide the source code for download so you can just plug it in to whatever you’re working on.

First off, what we’re going to be doing is switching out the font of superscripted/subscripted areas for a font that is superscripted/subscripted. For this, I’m using GG Superscript and GG Subscript. Download them!

Also, I’m using Flex Builder 3 and I’ll be building this as a Library Project. So keep that in mind.

Step One: Embed The Font

Put the fonts in a single Font SWF and then embed that SWF multiple times. If you don’t know how to make a Font SWF don’t worry, I’ll be making another post on how to do it. Or you can just Google it.

[Embed(source="../embedded/fonts.swf", fontName="GG Superscript")]
public static const superscriptFont:Class;

Step Two: Search

In order to know when to superscript we’ll need to search through the text for a certain tag. We could use <sup></sup> but if you’re already doing htmlText on your selected TextField then that tag will get ripped out before you have a chance to look for it. So I’m going to use [sup][/sup] so it will work in htmlText and normal text instances.

public static function superscript(tf:TextField):void
{
	var regExp:RegExp = /[sup](.*?)[/sup]/i;
	var result:Object = regExp.exec(tf.text);
 
	if(result)
	{
		// do it again recursively
		TextFieldUtil.superscript(tf);
	}
}

This is a recursive function because that RegEx is only going to match the first occurrence of the [sup] tags.

Step Three: Rip Out The Tags

After a match is found we have to rip out the actual tags from around the text but we also have to keep the any TextFormats intact. So, all we have to use is the replaceText function.

if(result)
{
	// rip out the tags and preserve all other text formatting
	tf.replaceText(result.index, result.index + result[0].length, result[1]);
 
	// do it again recursively
	TextFieldUtil.superscript(tf);
}

Step Four: Change The Format

Here’s where the text actually gets superscripted. All we have to do is swap out the font for that chunk of text.

if(result)
{
	// rip out the tags and preserve all other text formatting
	tf.replaceText(result.index, result.index + result[0].length, result[1]);
 
	// actually superscript the text
	tf.setTextFormat(	new TextFormat(	"GG Superscript",
						tf.defaultTextFormat.size,
						tf.defaultTextFormat.color,
						tf.defaultTextFormat.bold,
						tf.defaultTextFormat.italic,
						tf.defaultTextFormat.underline,
						tf.defaultTextFormat.url,
						tf.defaultTextFormat.target,
						tf.defaultTextFormat.align,
						tf.defaultTextFormat.leftMargin,
						tf.defaultTextFormat.rightMargin,
						tf.defaultTextFormat.indent,
						tf.defaultTextFormat.leading),
				result.index,
				result.index + result[1].length);
 
	// do it again recursively
	TextFieldUtil.superscript(tf);
}

It might look like a lot of code, but it’s really just one line that we added, there just happens to be a lot of typing, plus it’s broken out so it’s easier to read.

Conclusion

That’s pretty much it. Go ahead and download my textfieldutil so you can see how it all fits together.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • email
  • Ping.fm
  • Reddit
  • StumbleUpon
  • TwitThis
This entry was posted in Actionscript and tagged , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

4 Comments

  1. Posted October 17, 2008 at 3:37 pm | Permalink

    Great post. I work for a company that builds a lot of products for book publishers and we’ve had to support this as well. One of the things we’ve done is extended the TextArea component to use its stylesheet to automatically use an embedded font for super and subscripts.

    You can see more at:
    http://tdotblog.info/?q=node/24

    I’d love to get your thoughts on this.

  2. drich811
    Posted November 7, 2008 at 11:46 am | Permalink

    PROBLEM: I’m using FLASH with a dynamic HTML text field. The dynamic text is in HTML. There are several instances where I need to show superscript and subscript type.
    I’ve already download the GG superscript and subscript font. In order for the superscript and subscript to work, I need to embed fonts; however when I embed fonts tags and tags are ignored. In fact the and tagged text (this includes CSS with bold or ital rules) completely disappears.

    Does anyone have a solution to this problem?

    In english please (I’m new to code language) :)

  3. drich811
    Posted November 7, 2008 at 11:49 am | Permalink

    PROBLEM: I’m using FLASH with a dynamic HTML text field. The dynamic text is in HTML. There are several instances where I need to show superscript and subscript type.
    I’ve already download the GG superscript and subscript font. In order for the superscript and subscript to work, I need to embed fonts; however when I embed fonts

    tags and

    tags are ignored. In fact the

    ” and

    tagged text (this includes CSS with bold or ital rules) completely disappears.

    Does anyone have a solution to this problem?

  4. mkarthy
    Posted March 26, 2009 at 1:16 pm | Permalink

    Embeded superscript font “GG Superscript” not working in flex 3 Text control

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> <pre lang="" line="" escaped="">

Subscribe without commenting