我正在使用字符串数组集合填充下拉列表。我希望下拉列表控件的宽度与数组集合中最长字符串的大小(以像素为单位)相匹配。但我面临的问题是:集合中字符串的字体宽度不同,例如,“W”看起来比“l”更宽。因此,我估计字符宽度为8个像素,但这并不精确。如果遇到包含许多“W”和“M”的字符串,则估计就会出错。因此,我需要准确的像素字符串宽度。如何获得字符串的精确像素长度?
我的解决方案是将所有字符估计为8个像素宽:
任何想法或解决方案都非常重要。
谢谢。
我的解决方案是将所有字符估计为8个像素宽:
public function populateDropDownList():void{
var array:Array = new Array("o","two","three four five six seven eight wwww");
var sampleArrayCollection:ArrayCollection = new ArrayCollection(array);
var customDropDownList:DropDownList = new DropDownList();
customDropDownList.dataProvider=sampleArrayCollection;
customDropDownList.prompt="Select ...";
customDropDownList.labelField="Answer Options:";
//calculating the max width
var componentWidth=10; //default
for each(var answerText in array){
Alert.show("Txt size: "+ answerText.length + " pixels: " + answerText.length*9);
if(answerText.length * 8 > componentWidth){
componentWidth=answerText.length * 8;
}
}
customDropDownList.width=componentWidth;
answers.addChild(customDropDownList);
}
任何想法或解决方案都非常重要。
谢谢。