问题
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