2009-04-14 23 views
0

我的Flex應用程序中有許多標籤上的「truncateToFit」屬性設置爲true。問題在於,不是在任何截斷文本的末尾顯示「...」,而是顯示爲空。標籤組件上的Flex/Actionscript truncateToFit

即使我的標籤文本是「Hello Stackoverflow!」我可能希望我的標籤,看起來像這樣:

「你好Stackov ......」

相反,它顯示

「你好Stackovnull」

任何幫助將是有益的!

Example of how truncation should look

編輯: - 示例代碼:

<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> 
+0

請完全按照原樣添加源代碼(這看起來像一個錯誤之一)。標籤的文本是兩個字符串的串聯嗎?另外,你有`maxWidth`設置? – dirkgently 2009-04-14 16:14:58

+0

添加的代碼 - 我很確定這與我的代碼無關。這似乎可能是一個配置問題? – 2009-04-14 17:14:20

回答

2

哈哈!我找到了解決方案。對不起 - 這可能是我缺乏的信息,這使得你很難調試m :(

所以,無論如何 - 事實證明,我有一個外部resourceModule SWF,我的應用程序加載以獲得本地化的語言數據等from - 這個文件沒有包含關於截斷顯示哪些文本的一些數據(即'...'),所以它顯示爲'null'。我將這些數據添加到Resource swf中,並且它的所有工作都按預期工作。

萬分感謝試圖幫助我走出傢伙。)

0

我只是想你的示例代碼和它工作得很好。你確定它不是別的嗎?

0

所以我去了,並嵌入了我自己的字體,它截斷很好,沒有任何特別的問題。我不知道你是如何嵌入你的字體,但這種方法爲我工作。如果你正在做一些完全不同的事情,請在你的文章中指定。

// 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"/> 
0

我有這個問題今天(3H),這是太多了一點問題,因爲這個巨大的戰鬥。無論如何,上述提示都沒有解決我的問題。我嘗試了一切。我最終做了我自己的課程,它擴展了mx.controls.Label課程。執行如下。隨意在您的項目中使用它。請注意,在使用此函數時,應該禁用mxml中的truncateToFit。否則「null」字符串將被附加到您的文本中,並且不會進行截斷。

代碼:

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; 
     } 

    } 
}