自动调整文本以适应一个按钮的宽度 [英] Autosize text to fit the width of a button

查看:274
本文介绍了自动调整文本以适应一个按钮的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这篇关于自动调整文本以适应一个按钮的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆