how to let textfield's width auto fit the text?

放肆的年华 提交于 2019-12-24 07:35:16

问题


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.


回答1:


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;



回答2:


You can also use, String#length property to get length of text.

var str:String = "some string data";
trace(str.length);


来源:https://stackoverflow.com/questions/13024615/how-to-let-textfields-width-auto-fit-the-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!