The following example shows you how you create a custom Label control in Flex that masks its text as a password by setting the Label control’s nested protected UITextField control’s displayAsPassword property.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/27/displaying-a-label-controls-text-as-a-password/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        xmlns:local="*"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:HBox>
        <mx:Label text="Password:" />
        <local:MyLabel id="lbl"
                text="The quick brown fox jumped over the lazy dog."
                truncateToFit="true"
                width="100"
                creationComplete="lbl.tf.displayAsPassword = true;" />
    </mx:HBox>

</mx:Application>

View MyLabel.mxml

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/27/displaying-a-label-controls-text-as-a-password/ -->
<mx:Label xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:Script>
        <![CDATA[
            import mx.core.IUITextField;

            public function get tf():IUITextField {
                return textField;
            }
        ]]>
    </mx:Script>

</mx:Label>

View source is enabled in the following example.

As you can see, the Label control’s text is masked, but rolling over the label displays the masked text, because the truncateToFit property is set to true.

Note that the truncated Label text does not display the traditional ellipsis (“…”) indicating that the text has been truncated. That is because the ellipsis is also displayed as password text (masked as “*”).

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

4 Responses to Displaying a Label control’s text as a password

  1. dormouse says:

    ‘import mx.core.IUITextField;’

    maybe is ‘import mx.core.UITextField’. I can’t find out IUITextField Class.

    dormouse

  2. peterd says:

    dormouse,

    Are you using Flex 2 or Flex 3? I only tested this in Flex 3 (with the latest nightly SDK build).

    Peter

  3. dormouse says:

    Sorry,
    yes, you are right.i am using Flex3 b2 not Flex3 b3

  4. Works fine in Flex 3.

Leave a Reply

Your email address will not be published.

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="">

Anti-Spam Protection by WP-SpamFree