views:

1757

answers:

7

I have a number of labels in my Flex application which have the "truncateToFit" property on them set to true. The problem is that, instead of displaying '...' at the end of any truncated text it displays null.

I.e if my label's text was: "Hello Stackoverflow!" I might expect my label to look like this:

"Hello Stackov..."

Instead it displays

"Hello Stackovnull"

Any help would be useful!

Example of how truncation should look

Edit: - Example code:

<mx:HBox width="200" ... >
   <mx:Label maxWidth="150" truncateToFit="true" text="Really long text.Really long text.Really long text.Really long text.Really long text" />
</mx:HBox>
A: 

I just tried your example code and it worked fine. Are you sure its not something else?

rogueg
A: 

So I went and embedded a font of my own and it truncates nicely without any particular issue. I'm not sure how you are embedding your font but this method worked for me. If you are doing something completely different then please specify in your post.

// Cannot name the font as one that already exists!
[Embed(source="Anonymous.ttf", fontFamily="myAnon")]
private var fontA : Class;

[Embed(source="HGRSGU.TTC", fontFamily="myFont")]
private var fontB : Class;

//...I have some code here that switches the font
var obj : Object = truncateMe.getStyle("fontFamily");
if (obj == "myAnon")
  truncateMe.setStyle("fontFamily", "myFont");
else
  truncateMe.setStyle("fontFamily", "myAnon");

<!-- My Label -->
<mx:Label maxWidth="150" truncateToFit="true" id="truncateMe"
    text="Something really long goes here" fontFamily="myFont" fontSize="20"/>
nevets1219
Thanks for testing that out. I wonder if it has anything to do with the particular font I'm using? Though I know it has a full-stop character :s
Richie_W
If you don't mind sharing the font, others can try to figure out if the issue is specific to your end or not but I know for sure this method works (at least with these two fonts that I'm using). You can probably work around the issue by manually coding the behavior in (I think).
nevets1219
+1  A: 

Ha ha! I found the solution. Sorry guys - It was probably my lack of information which made it hard for you guys to debug for m :(

So, anyway - It turns out I had an external resourceModule swf which my application loaded to get localised language data etc from - This file didn't include the some data about what text to display for the truncation (i.e. '...') and so it instead displayed 'null'. I added that data to the Resource swf and it's all working as expected.

Thanks a million for trying to help me out guys. ;)

Richie_W
A: 

Hey Richie, how did you add the date to the resource swf? I am having the same problem.

A: 

If working with multiple locales, make sure you add 'en_US' to your localeChain. E.g.: resourceManager.localeChain = ['pt_BR', 'en_US'];

Found solution at: http://blog.flexexamples.com/2008/01/26/truncating-text-in-the-flex-label-control-using-the-truncatetofit-property/

Look for Leandro's posting

P120D1GY
A: 

Well hello,

I had a huge fight with this issue today (3h), which is way too much for a little issue as this. Anyway, none of the above tips solved my problem. I tried it all. I ended up doing my own class, which extends the mx.controls.Label class. The implementation is below. Feel free to use it in your projects. Please note that you should disable the truncateToFit in your mxml when using this one. Else the "null" string will be appended to your text and no truncation will be made.

Code:

package com.feijk.UI{ import mx.controls.Label;

/**
 * An extension for mx.controls.Label to truncate the text and show
 * a tooltip with the full-length content. This sub-class is meant to be
 * used when the regular truncateToFit does result in a "null" appendix
 * on the string instead of the "...". In order for this to work, I used
 * the following parameters in my mxml: 
 * 
 *  - truncateToFit = false
 *  - maxWidth = set
 *  - width = set
 * 
 * 
 * @author Tomi Niittumäki // Feijk Industries 2010
 * @NOTE: Feel free to use! :)
 */
public class FLabel extends Label{

    // define the truncation indicator eg. ...(more) etc.
    private const TRUNCATION_INDICATOR:String = new String("...");

    /**
     * Constructor
     */
    public function FLabel(){
        super();
    }

    /**
     * The overriding method, which forces the textField to truncate
     * its content with the method truncateToFit(truncationIndicator:String)
     * and then supers the tooltip to be the original full-length text.
     */
    override protected function updateDisplayList(unscaledWidth:Number, 
            unscaledHeight:Number):void{
        super.updateDisplayList(unscaledWidth, unscaledHeight);
        //trace("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!: "+textField.text);
        textField.truncateToFit(TRUNCATION_INDICATOR);
        super.toolTip = text;
    }

}

}

Tomi Niittumäki
+1  A: 

The problem is that one of the default Flex resource bundles is not included in your compiled localization files. See here for the detailed explanation and fix: http://deniszgonjanin.posterous.com/flex-truncation-null-error-fix

Denis