Displaying a Label control’s text as a password

by Peter deHaan on January 27, 2008

in Label

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 “*”).

{ 4 comments… read them below or add one }

1 dormouse January 27, 2008 at 11:30 pm

‘import mx.core.IUITextField;’

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

dormouse

Reply

2 peterd January 28, 2008 at 1:37 pm

dormouse,

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

Peter

Reply

3 dormouse January 28, 2008 at 7:33 pm

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

Reply

4 Srikanth Dhondi May 10, 2009 at 12:13 pm

Works fine in Flex 3.

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: