02
Aug
07

Using special characters in your Flex applications

Another good question/dilemma came up on FlexCoders the other day. A curious poster asked how you could embed a copyright symbol, ©, into a Flex application since using © did not seem to work. As it turns out, an easy way to do this is to use a character’s numeric value (©) instead of its code name (©).

Well, at any rate, here’s a handy little chart thing-a-ma-jig to hopefully help out if you’re looking for some specific codes. Granted, you could have probably found it on Google in a fraction of the time, but what fun would that be?

Full code after the jump.

Clicking on a row in the DataGrid control will display both the numeric and string values, for easy copy-paste goodness. Changing the values of the Slider control will change which characters are displayed. By default characters from   to Ȁ are displayed.

Download source (ZIP, 1K) | View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/02/using-special-characters-in-your-flex-applications/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init()">

    <mx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;

            [Bindable]
            private var charCodes:ArrayCollection;

            private function init():void {
                charCodes = new ArrayCollection();
                var i:int;
                for (i = slider.values[0]; i <= slider.values[1]; i++) {
                    charCodes.addItem({charCodeNum:i, charCodeValue:"&#" + formatString(i) + ";",  charCodeStr:String.fromCharCode(i)});
                }
            }

            private function formatString(str:Object, minLength:int = 4):String {
                return ("000000000" + str.toString()).substr(-minLength);
            }
        ]]>
    </mx:Script>

    <mx:VBox>
        <mx:DataGrid id="dataGrid" dataProvider="{charCodes}" width="300">
            <mx:columns>
                <mx:DataGridColumn dataField="charCodeStr" />
                <mx:DataGridColumn dataField="charCodeValue" />
            </mx:columns>
        </mx:DataGrid>

        <mx:HBox width="100%">
            <mx:HSlider id="slider" minimum="32" maximum="512" thumbCount="2" values="[0, 512]" liveDragging="true" snapInterval="1" tickInterval="32" dataTipPrecision="0" change="init()" />
            <mx:Label text="`{dataGrid.selectedItem.charCodeStr}` = {dataGrid.selectedItem.charCodeValue}" selectable="true" />
        </mx:HBox>
    </mx:VBox>

    <mx:Label text="&#169; {new Date().fullYear} Flex Examples" />

</mx:Application>

View source is enabled in the following example.

Update 2007/09/01: I just discovered another trick today. You can use custom XML entities which allow you to use “&copy;” from within your Flex applications instead of “&#169;” or “&#xA9;”:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/02/using-special-characters-in-your-flex-applications/ -->
<!DOCTYPE example[
    <!ENTITY copy "&#xA9;">
]>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Label text="&copy;" fontSize="64" />

</mx:Application>

Update 2008/08/19: You can also use special characters in your Flex applications using the following syntax: &#169; (MXML), &#xA9; (MXML), and \u00A9 (ActionScript), as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/02/using-special-characters-in-your-flex-applications/ -->
<mx:Application name="Button_label_Unicode_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="horizontal"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            private function init():void {
                btn3.label = "\\u00A9 - \u00A9 (ActionScript)";
            }
        ]]>
    </mx:Script>

    <mx:Button id="btn1" label="&#169; - © (MXML)" />
    <mx:Button id="btn2" label="&#xA9; - © (MXML)" />
    <mx:Button id="btn3" creationComplete="init();" />

</mx:Application>

View source is enabled in the following example.


9 Responses to “Using special characters in your Flex applications”


  1. 1 Oğuz Demirkapı Aug 2nd, 2007 at 9:16 pm

    Nice to see this sample. Thanks!

    But as I know we can directly use Unicode chars without any need of any kind of replacement if you have the right font.

  2. 2 Theo Aug 3rd, 2007 at 5:04 am

    The translation between &#xxxx; and characters seems to be done at compile time, most likely by the XML parser. This is a bummer if you load data from a database and want to display some special characters.

    I’ve been desperately trying to create a currency formatter which gets it’s currency symbol via a remote object call to display a € (Euro symbol, char code 8364). Can’t get it to work… all I get is a â (a with circumflex).

    Your method works fine if I do this:

    But not if I do this:

    var cf : CurrencyFormatter = new CurrencyFormatter();
    cf.currencySymbol = "€"
    

    (and because of that, loading a string containing “&#8364″ obviously wouldn’t work either)

    You would have to do this (which doesn’t work so well when you load your data externally, it’s not feasible to scan through all text looking for entities):

    var cf : CurrencyFormatter = new CurrencyFormatter();
    cf.currencySymbol = String.fromCharCode(8364);
    

    As it is I have given up, other currencies are written with their three-letter abbreviations anyway, so writing EUR 33.00 is perfectly fine. However, it would look so much better with a proper €. If you have any similiar ideas of how to get special characters to work in an even more general way I would be very interested.

  3. 3 peterd Aug 3rd, 2007 at 8:39 pm

    Theo,

    How is it not working?

    If I try this in my MXML document it seems to work:

    <mx:CurrencyFormatter id="cf" currencySymbol="&#8364;" precision="2" />
    <mx:Text htmlText="{cf.format(100)}" />
    

    Also, if I load a remote document (content.txt) it looks like it parses that € character properly (as long as I set the htmlText property and not the text property). Observe:

    <mx:HTTPService id="getText" url="content.txt" resultFormat="text" />
    <mx:Text condenseWhite="true" creationComplete="getText.send()">
        <mx:htmlText>{getText.lastResult}</mx:htmlText>
    </mx:Text>
    

    And my content.txt file looks like:

    <p>I'm a big fancy eurosymbol: &#8364;</p>
    

    Is that roughly what you were looking for, or did I totally misunderstand?

    Peter (who hopes the formatting for this turned out)

  4. 4 Theo Aug 5th, 2007 at 6:51 am

    Yes, it works in a MXML document, because the XML parser translates &#8364; into €, but it doesn’t work if you put the &#8364; in ActionScript code, or if you load it using remote objects. Like this (in an .as or a script tag):

    textField.text = "&#8364;";
    

    That will display the literal string in the text field, however

    works fine.

    I hadn’t considered htmlText though, good idea. It should work regardless.

  5. 5 Quentin Dec 4th, 2007 at 5:48 am

    To properly display the euro character from strings got via a RemoteObject (using AMFPHP) I simply used this line of code :

    myString=myString.replace(String.fromCharCode(128), "€");
    

    And oddly enough, this works…
    Instead of a weird square character, I get the nice ‘€’ one!

  6. 6 jbishop Dec 13th, 2007 at 12:02 pm

    nice! It is also critical that you have the special characters embedded in the font you’re using.

  7. 7 Quentin Dec 18th, 2007 at 5:56 am

    Well, no…
    At least not for my snippet.

  8. 8 Jack Nov 17th, 2008 at 4:28 am

    Hi.
    Just looking for a special character for a “does not equal” eg. an equals sign with a slash going through it…do you have any idea if one exists? Thanks

  9. 9 peterd Nov 17th, 2008 at 7:59 am

    Jack,

    On Windows you can use charmap.exe to find all sorts of special characters and their unicode values (which may or may not be different/available, depending on which font you’re using).

    The character you want is “Not Equal To”, and is U+2260: ࣔ

    Peter

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".