如何让文本框的宽度自动适合文本? [英] how to let textfield's width auto fit the text?

查看:288
本文介绍了如何让文本框的宽度自动适合文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我正在考虑制作一个类似于html表格标签的组件。

html表格可以自动适合单元格的宽度和文本或内容。列的宽度取决于该列上最宽的单元格。

 < table> 
< tr>
< td>这可能会很长。< / td>
< / tr>
< tr>
< td>短< / td>
< / tr>
< / table>

但是在as3这是一个问题,我不知道字符串的实际大小,除非我设置它到一个文本框。我不认为这是一个好的策略,使用实际的textfield实例尝试和调整大小,所以让我知道,如果你有一个更好的办法。

解决方案

首先,您需要启用自动调整大小:

  textField.autoSize = TextFieldAutoSize.LEFT ; 

然后,如果要读取产生的文字大小,可以使用:

  var width:Number = textField.textWidth; 
var height:Number = textField.textHeight;


recently i was thinking making a component which like html's table tag.

html table can auto fit the cell's width with the text or content in it.and the certain column's width is decided by the widest cell on that column.

<table>
    <tr>
        <td>this could be very long.</td>
    </tr>
    <tr>
        <td>short</td>
    </tr>
</table>

but in as3 this is a problem,i don't know the string's actual size unless i set it to a textfield. i don't think it would be a good strategy to use the actual textfield instance try and resize, so let me know if you have a better way.

解决方案

First of all, you need to enable auto-sizing:

textField.autoSize = TextFieldAutoSize.LEFT;

Then, if you want to read the resulting text size use can use:

var width:Number = textField.textWidth;
var height:Number = textField.textHeight;

这篇关于如何让文本框的宽度自动适合文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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