【问题标题】:Dynamic font size Flex 4 to resize when window/panel is resized动态字体大小 Flex 4 在调整窗口/面板大小时调整大小
【发布时间】:2011-06-25 18:26:16
【问题描述】:

我有一个 Flex 自定义 BorderContainer 组件,其中包含文本。如果我要将它添加到面板内的主应用程序中,则 BorderContainer 会超出面板的宽度范围,因为文本是设定的大小。我将其所有组件都以百分比表示,以便在缩小时重新调整大小,但是包含复选框和标签(很多带有文本的东西)的部分会因为字体大小没有改变而混乱。

我非常肯定我正在寻找的结果可以通过嵌入字体来完成,尽管我无法从网上提出解决方案。我正在尝试使用 CSS 样式来做到这一点,因为我会将它用于许多不同的组件(我不只是想直接在 flex 代码中更改它)。

编辑解决方案:

我尝试使用www0z0k 建议的比率,但是当它快速调整大小或小屏幕时会导致一些严重问题(组件不会正确调整大小,因为它乘以比率。最终似乎对我有用并且没有引起任何问题的是我运行了代码并找到了容器的width (1152) 和 height (842)。

然后我创建了一个 widthX = 1152 和 heightY = 842 的 const 变量,并在 onResize() 函数中对调整大小进行了编码,如下所示:
(其中bottomGroup 是我要调整大小的borderContainer 的id)

bottomGroup.scaleX = width / widthX;;
bottomGroup.scaleY = height / heightY;

结束编辑

到目前为止,我在<fx:Style> 中找到了一些嵌入字体的示例,并试图删除对 fontSize 的任何声明,但这似乎不起作用。

<fx:Style>
    @namespace s "library://ns.adobe.com/flex/spark";

    @font-face {
        src: url("fonts\Arial.ttf");
        fontFamily: myFontFamily;
        color: black;
        fontStyle: normal;
        fontWeight: normal;
        advancedAntiAliasing: false;
    }

    s|BorderContainer {
        fontFamily: myFontFamily;
    }   
</fx:Style>

如果有人有任何建议或知道我哪里出错了,我们将不胜感激。谢谢。

编辑:显示我的问题的示例应用程序。
CustomContainer.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:BorderContainer xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:HGroup>
    <s:Label text="This is a label" />
    <s:Label text="Here is another which add up to take up quite a bit of horizontal space" />
    <s:Label text="Here is another which add up to take up quite a bit of horizontal space" />
    <s:Label text="Here is another which add up to take up quite a bit of horizontal space" />
</s:HGroup>

Main.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:local="*"
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

<fx:Style>
    @namespace s "library://ns.adobe.com/flex/spark";

    @font-face {
        src: url("fonts\Arial.ttf");
        fontFamily: myFontFamily;
        color: black;
        fontStyle: normal;
        fontWeight: normal;
        advancedAntiAliasing: false;
    }

    s|BorderContainer {
        fontFamily: myFontFamily;
    }   
</fx:Style>
<s:Panel title="BorderContainer Component Example"
         width="75%" height="75%" 
         horizontalCenter="0" verticalCenter="0"> 

    <local:container>

    </local:container>

</s:Panel>

我希望能够重新调整字体大小以适应面板。我无法提供我正在处理的实际代码,但这个例子希望能传达这个想法。

【问题讨论】:

  • 有什么问题?您能否发布更多代码,特别是您提到的面板和 BorderContainer 的代码?
  • 嗯,当然,等等。问题是您是否能够让文本重新调整大小以适应给定区域而不超出其边界
  • 发布了新代码,这只是一些快速的东西,但希望它能澄清问题。我希望文本重新调整大小以适合面板,而不是继续超出面板宽度。
  • 假设您的应用程序以等于 minWidth 值的宽度开始,您的 BorderContainer 超出面板宽度的原因是应用程序和 BC 都有主宽度,但您设置了面板拥有应用程序宽度的 75%。
  • 对不起,我没有注意到我放的代码,但我实际上拥有的是自定义容器有 width="100%" 和 height="100%" 并且它仍然是同样的问题

标签: css apache-flex fonts flex4 resize


【解决方案1】:

应该可以以类似的方式修改字体大小,但恕我直言,如果您有除文本以外的其他内容(您提到了复选框),您应该使用以下内容:

<?xml version="1.0" encoding="utf-8"?>
<s:BorderContainer xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%" resize="onResize();">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<fx:Script>
    <![CDATA[

    private function onResize():void{
        var ratio:Number = width / measuredWidth;
        contentHolder.scaleX *= ratio;
        contentHolder.scaleY *= ratio;
    }

    ]]>
</fx:Script>

<s:HGroup id="contentHolder">
    <s:Label text="This is a label" />
    <s:Label text="Here is another which add up to take up quite a bit of horizontal space" />
    <s:Label text="Here is another which add up to take up quite a bit of horizontal space" />
    <s:Label text="Here is another which add up to take up quite a bit of horizontal space" />
</s:HGroup>
</s:BorderContainer>

Main.xml 不需要修改

【讨论】:

    【解决方案2】:

    如果您在 BorderContainer 中的标签上设置了明确的宽度(例如 100%),它们应该会自动为您自动换行(请参阅:Label docs)。

    如果您想实际更改字体大小(我会避免这样做,因为它会在视觉上造成混淆,但这是另一个讨论)您应该能够更改标签的 scaleX 和 scaleY 属性,以便它们被缩放完全适合 BorderContainer。您需要做的就是计算比例并应用它。

    【讨论】:

    • 问题是它们是复选框和标签(在组等中)并且换行不是一个选项(因为我有一些复选框网格)所以我希望文本重新缩放。不过我会研究 scaleX 和 scaleY,我只是想知道是否有办法让它自动这样做或在 CSS 中完成
    • 据我所知,在 CSS 中没有办法做到这一点。
    猜你喜欢
    • 2011-02-28
    • 2011-08-18
    • 2021-04-06
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 2016-07-07
    • 2012-07-29
    • 2017-03-25
    相关资源
    最近更新 更多