Flex/Actionscript truncateToFit 在标签组件上

发布于 2024-07-16 17:22:45 字数 679 浏览 10 评论 0 原文

我的 Flex 应用程序中有许多标签,它们的“truncateToFit”属性设置为 true。 问题是,在任何截断文本的末尾不是显示“...”,而是显示 null。

即,如果我的标签文本是:“Hello Stackoverflow!” 我可能希望我的标签看起来像这样:

“Hello Stackov...”

相反,它显示

“Hello Stackovnull”

任何帮助都会有用!

截断的外观示例

编辑: - 示例代码:

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

扎心 2024-07-23 17:22:45

哈哈! 我找到了解决方案。 抱歉,各位 - 可能是我缺乏信息,导致你们很难调试 m :(

所以,无论如何 - 事实证明,我有一个外部资源模块 swf,我的应用程序加载了该资源以从中获取本地化语言数据等 - 此文件不包含有关截断显示的文本的一些数据(即“...”),因此它显示为“null”,我将该数据添加到资源 swf 中,一切都按预期工作

。试图帮助我的人;)

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. ;)

给我一枪 2024-07-23 17:22:45

问题是默认的 Flex 资源包之一未包含在您编译的本地化文件中。 请参阅此处了解详细说明和修复:
http://deniszgonjanin.posterous.com/flex-truncation-null-error-修复

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

尽揽少女心 2024-07-23 17:22:45

我刚刚尝试了你的示例代码,效果很好。 你确定这不是别的东西吗?

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

眼角的笑意。 2024-07-23 17:22:45

因此,我嵌入了自己的字体,它可以很好地截断,没有任何特殊问题。 我不确定你是如何嵌入字体的,但这种方法对我有用。 如果您正在做完全不同的事情,请在您的帖子中注明。

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

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"/>
习惯成性 2024-07-23 17:22:45

如果使用多个语言环境,请确保将“en_US”添加到您的语言环境链中。 例如:
ResourceManager.localeChain = ['pt_BR', 'en_US'];

找到解决方案:http://blog.flexexamples.com/2008/01/26/truncating-text-in-the-flex-label-control-using-the-truncatetofit-property/

寻找 Leandro 的发帖

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

把梦留给海 2024-07-23 17:22:45

今天(3小时)我为这个问题进行了一场激烈的斗争,对于像这样的小问题来说,这实在是太多了。 无论如何,以上提示都没有解决我的问题。 我都试过了。 我最终创建了自己的类,它扩展了 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;
        }

    }
}

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

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文