动态字体大小 Flex 4 在调整窗口/面板大小时调整大小
我有一个 Flex 自定义 BorderContainer 组件,其中包含文本。如果我要将其添加到面板内的主应用程序中,则由于文本的大小是固定的,BorderContainer 会超出面板的宽度范围。我将其所有组件都设置为百分比,以便在缩小时重新调整大小,但是包含复选框和标签(很多带有文本的内容)的部分会混乱,因为字体大小不会改变。
我非常肯定我正在寻找的结果可以通过嵌入字体来完成,尽管我无法从网上找到解决方案。我尝试使用 CSS 样式来做到这一点,因为我将在许多不同的组件中使用它(我不想直接在 Flex 代码中更改它)。
编辑解决方案:
我尝试使用www0z0k
建议的比例,但当它快速调整大小或调整到小屏幕时,它会导致一些严重的问题(该组件不会正确地重新调整大小,因为它乘以比率。最终似乎对我有用并且没有引起任何问题的是我运行了代码并找到了宽度(1152)和高度。 然后,我创建了一个 widthX = 1152 和 heightY =
842 的 const 变量,并在 onResize() 函数中对调整大小进行了编码:
(其中 BottomGroup 是我尝试调整大小的 borderContainer 的 id
)
bottomGroup.scaleX = width / widthX;;
bottomGroup.scaleY = height / heightY;
END EDIT
到目前为止,我已经在
并尝试删除 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>
<代码>
我希望能够调整字体大小以适应面板。我无法提供我正在处理的实际代码,但希望这个示例能够传达这个想法。
I have a Flex custom BorderContainer component which has text inside of it. If I were to add this in my main Application inside of a panel, the BorderContainer goes outside of the width bounds of the panel due to the text being a set size. I have all of its components in percentages so that it re-sizes when shrunk, but the one part that contains checkboxes and labels (lots of things with text) mess up since the font size doesn't change.
I am pretty positive that the results I am looking for can be done through embedding the font, though I have not been able to come up with a solution from online. I am trying to do this with a CSS style since I will be using it for many different components (I dont just want to change it in the flex code directly).
EDIT Solution:
I attempted to use the ratio as www0z0k
had suggested but it caused some serious issues when it was re-sized quickly or to a small screen (the component would not re-size correctly because it was multiplying by the ratio. What finally seems to have worked for me and caused no issues was that I ran the code and found the width
(1152) and height
(842) of the container.
I then created a const variable of widthX = 1152 and heightY = 842 and in the onResize() function I coded the resize like this:
(where bottomGroup is the id
of the borderContainer I am trying to resize)
bottomGroup.scaleX = width / widthX;;
bottomGroup.scaleY = height / heightY;
END EDIT
So far I have found some examples of embedding fonts in the <fx:Style>
and attempted to remove any delaration of fontSize but that doesn't seem to work.
<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>
If anyone has any suggestions or knows where I am going wrong, any help would be appreciated. Thanks.
EDIT: A sample application displaying what my issue is.
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>
I would like to be able to have the font re-size to fit into the panel. I couldn't provide the actual code that I am working on, but this example hopefully gets the idea across.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该可以以类似的方式修改字体大小,但恕我直言,如果您有除文本之外的其他内容(您提到了复选框),您应该使用以下内容:
Main.xml不需要修改
it should be possible to modify font size in a similar way, but imho if you have something except text there (you mentioned checkboxes) you sholud use the following:
Main.xml doesn't need to be modified
如果您在 BorderContainer 中的标签上设置显式宽度(例如 100%),它们应该会自动为您换行(请参阅:标签文档)。
如果您想实际更改字体大小(我会避免这样做,因为它会在视觉上造成混乱,但这是另一个讨论)您应该能够更改标签的scaleX和scaleY属性,以便它们能够缩放以完美贴合在边界容器中。您需要做的就是计算比例并应用它。
If you set an explicit width (e.g., 100%) on your Labels in BorderContainer they should automagically do line wraps for you (see: Label docs).
If you want to actually change the font size (and I would avoid this since it will be visually confusing, but that's another discussion) you should be able to change the scaleX and scaleY properties of your Labels such that they will be scaled to fit perfectly in the BorderContainer. All you'll need to do is compute the scale and apply it.