自动调整文本以适应一个按钮的宽度宽度、按钮、文本、以适应

2023-09-09 21:46:42 作者:亡者的凝视

我有一个很好的环顾四周,但没有找到任何直接解决了我的问题。有几个职位它们是沿着正确的方向,但我不能让我的头周围,以解决该问题。

I've had a good look around but couldn't find anything that directly solves my problem. There are several posts which are along the right lines, but I can't get my head around it in order to fix the issue.

我需要自动调整大小的文本,使其不超过按钮的宽度。最大长度为我的大文字显示为13个字符的时候,才变得太大。

I need to automatically resize the text so that it doesn't exceed the width of the button. The maximum length for my sized text appears to be 13 characters before it will become too large.

-

程序开始通过动态创建多个按钮,在XML文档中设置标题和描述填充它们。

The program starts by dynamically creating several buttons, filling them with the title and description as set in an XML document.

要加我有这个code中的文本:

To add the text I have this code:

var tform:TextFormat = new TextFormat();
tform.size = 20;
tform.font = "Arial";
tform.align = TextFormatAlign.CENTER;
tform.color = 0xFFFFFF;
tform.bold = true;

var tfield:TextField = new TextField();
tfield.text = texttitle;
tfield.width = button.width;
tfield.x = 0;
tfield.y = 30;

tfield.setTextFormat(tform);
addChild(tfield);

此直接在从顶部的按钮30的像素的中心位置的文本。因此,我所面临的问题是如何改变字体大小,以保持在框中的文本。

This positions the text directly in the centre of the button 30 pixels from the top. The problem I therefore face is how to modify the font size in order to keep the text within the box.

-

现在,因为我设置宽度为面板的尺寸,我看不出遍历根据允许的最大宽度设置字体大小的任何明显的方法。

Now, because I set the width to the the size of the panel, I can't see any obvious way of looping through to set the font size according to the maximum allowed width.

我相信这一切是有道理的,我欢迎任何修改,以使code更有效,因为我是比较新的AS3和Flash,因此我仍然在学习曲线。

I trust this all makes sense, and I welcome any modifications to make the code more efficient as I'm relatively new to AS3 and Flash and therefore am still on the learning curve.

问候, 乔恩。

推荐答案

我过这个问题跑,并通过循环文本设置,直到它符合解决它

I've ran across this problem and solved it by looping over the text setting until it fits

沿此线的东西

while (tf.textwidth > button.width){
   myTextFormat.size = myTextFormat.size - 1;
   tf.setTextFormat(myTextFormat);
   tf.autoSize = "left";
}
 
精彩推荐
图片推荐