Adobe Flex - 有什么方法可以自动更改字体大小以适合其容器吗?

发布于 2024-08-04 14:31:12 字数 53 浏览 4 评论 0原文

我在 Flex 中有一个组件,该组件的一部分是标签。有没有办法自动调整字体大小以适合其容器?

I have a component in Flex, and part of that component is a label. Is there a way to automatically adjust the font size to fit into its container?

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

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

发布评论

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

评论(2

吐个泡泡 2024-08-11 14:31:12

不会自动(至少据我所知),没有设置某种事件处理程序,并对某些事件的发生采取行动。下面是一个简单的 AIR 应用程序,它演示了一种方法 - 在本例中,调整大小事件会触发标签的 fontSize 更改:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="this_creationComplete()">

    <mx:Script>
        <![CDATA[

            import mx.binding.utils.BindingUtils;
            import mx.binding.utils.ChangeWatcher;

            [Bindable]
            public var myValue:int = 0;

            private function this_creationComplete():void
            {
                setSize();

                addEventListener(Event.RESIZE, handleResize);
            }

            private function handleResize(event:Event):void
            {
                setSize();
            }

            private function setSize():void
            {
                lbl.setStyle("fontSize", this.height / 2);
            }

        ]]>
    </mx:Script>

    <mx:Label id="lbl" text="Hello, world!" />

</mx:WindowedApplication>

这里,当调整应用程序大小时,标签的 fontStyle 属性将更改为应用程序高度的一半;放大或缩小它,你就会看到它是如何工作的。当然还有其他方法,一如既往,但因为我们处理的是样式而不是可绑定属性,所以很可能需要一些自定义编码。希望有帮助!

Not automatically (at least not that I know of), without setting up some sort of event handler, and taking action on the occurrence of some event. Here's a simple AIR app that demonstrates one approach -- in this case, a resize event triggering a change in the fontSize of a Label:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="this_creationComplete()">

    <mx:Script>
        <![CDATA[

            import mx.binding.utils.BindingUtils;
            import mx.binding.utils.ChangeWatcher;

            [Bindable]
            public var myValue:int = 0;

            private function this_creationComplete():void
            {
                setSize();

                addEventListener(Event.RESIZE, handleResize);
            }

            private function handleResize(event:Event):void
            {
                setSize();
            }

            private function setSize():void
            {
                lbl.setStyle("fontSize", this.height / 2);
            }

        ]]>
    </mx:Script>

    <mx:Label id="lbl" text="Hello, world!" />

</mx:WindowedApplication>

Here, when the app gets resized, the label's fontStyle property gets changed to one-half the app's height; scale it up and down and you'll see how it works. There are certainly other approaches, as always, but because we're dealing with styles and not bindable properties, a little custom coding is most likely called for. Hope it helps!

神妖 2024-08-11 14:31:12

如果您指的是单行标签,那么这个组件可能会对您有所帮助。考虑到它只能使其字体变小(作为对容器宽度减小的反应)。

import flash.events.Event;
import flash.text.TextLineMetrics;

import mx.controls.Label;
import mx.events.FlexEvent;

public class ElasticLabel extends Label {
    private static const EXTRA_SPACE:Number = 2;

    private static const MIN_FONT_SIZE:Number = 6;

    private var fontResizeRequired:Boolean = false;

    public function ElasticLabel() {
        super();

        addEventListener("explicitMaxWidthChanged", triggerFontResize);
        addEventListener(FlexEvent.VALUE_COMMIT, triggerFontResize);
    }

    private function triggerFontResize(event:Event):void {
        fontResizeRequired = true;
    }

    private function get currentWidth():Number {
        var textMetrics:TextLineMetrics = measureText(text);
        var textWidth:Number = textMetrics.width;
        var paddings:Number = 0;
        var paddingLeft:Number = getStyle("paddingLeft");
        if (!isNaN(paddingLeft)) paddings += paddingLeft;
        var paddingRight:Number = getStyle("paddingRight");
        if (!isNaN(paddingRight)) paddings += paddingRight;
        return textWidth + paddings + EXTRA_SPACE;
    }

    private function resizeFont():void {
        if (!isNaN(maxWidth) && maxWidth > 0) {
            var fontSize:Number = getStyle("fontSize") as Number;
            while (maxWidth < currentWidth 
                    && fontSize > MIN_FONT_SIZE) {
                fontSize -= 0.5;
                setStyle("fontSize", fontSize);
            }
        }
    }

    override protected function updateDisplayList(unscaledWidth:Number, 
            unscaledHeight:Number):void {
        super.updateDisplayList(unscaledWidth, unscaledHeight);

        if (fontResizeRequired) {
            resizeFont();
            fontResizeRequired = false;
        }
    }
}

If you mean a single-line label then this is the component that may help you. Consider that it can only make it's font size smaller (as a reaction to container's width reduction).

import flash.events.Event;
import flash.text.TextLineMetrics;

import mx.controls.Label;
import mx.events.FlexEvent;

public class ElasticLabel extends Label {
    private static const EXTRA_SPACE:Number = 2;

    private static const MIN_FONT_SIZE:Number = 6;

    private var fontResizeRequired:Boolean = false;

    public function ElasticLabel() {
        super();

        addEventListener("explicitMaxWidthChanged", triggerFontResize);
        addEventListener(FlexEvent.VALUE_COMMIT, triggerFontResize);
    }

    private function triggerFontResize(event:Event):void {
        fontResizeRequired = true;
    }

    private function get currentWidth():Number {
        var textMetrics:TextLineMetrics = measureText(text);
        var textWidth:Number = textMetrics.width;
        var paddings:Number = 0;
        var paddingLeft:Number = getStyle("paddingLeft");
        if (!isNaN(paddingLeft)) paddings += paddingLeft;
        var paddingRight:Number = getStyle("paddingRight");
        if (!isNaN(paddingRight)) paddings += paddingRight;
        return textWidth + paddings + EXTRA_SPACE;
    }

    private function resizeFont():void {
        if (!isNaN(maxWidth) && maxWidth > 0) {
            var fontSize:Number = getStyle("fontSize") as Number;
            while (maxWidth < currentWidth 
                    && fontSize > MIN_FONT_SIZE) {
                fontSize -= 0.5;
                setStyle("fontSize", fontSize);
            }
        }
    }

    override protected function updateDisplayList(unscaledWidth:Number, 
            unscaledHeight:Number):void {
        super.updateDisplayList(unscaledWidth, unscaledHeight);

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