字符串的长度(以像素为单位)
我正在使用字符串的 arrayCollection 填充 dropDownList 。我希望下拉列表控件的宽度与数组集合中最长字符串的大小(以像素为单位)匹配。我面临的问题是:集合中字符串的字体宽度不同,例如“W”看起来比“l”宽。所以我估计字符的宽度为 8 像素,但这不太整洁。如果遇到具有许多“W”和“M”的字符串,则估计是错误的。所以我想要字符串的精确像素宽度。我怎样才能得到字符串的确切长度(以像素为单位)?
我的解决方案将所有字符估计为 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);
}
任何想法或解决方案都非常有价值。
谢谢
I'm populating a dropDownList with arrayCollection of strings. I want the width of the drop down list control to match with the size (in pixels) of the longest string in the array collection. The problem I'm facing is: the font width of the strings in the collection are different e.g. 'W' looks wider than 'l'. So I estimated the width of a character to be 8 pixels but that's not pretty neat. If a string that has many 'W' and 'M' is encountered the estimation is wrong. So I want precise pixel width of strings. How can i get the exact length of a string in pixels??
My solution that estimates all character to be 8 pixels wide is given below:
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);
}
Any idea or solution is highly valued.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了获得更准确的测量结果,您可以使用字符串填充 TextField,然后测量该 TextField 文本的宽度。
代码:
用法:
To get a more accurate measurement, you can populate a TextField with the string, then measure the width of that TextField's text.
Code:
Usage:
我对其他人的帖子没有编辑权限,所以我将其作为单独的答案发布,但如果这有效,功劳应该归于卡梅伦:
如果我看到它确实有效并且他的帖子被编辑了,我会删除这个以保持清洁。
抱歉,垃圾帖子最初是试图回答这个问题,只是错误地回答了...无论如何测试了这个,它似乎有效。我在 Flex 中做到了这一点,但你应该能够使用 AS3 部分,没问题,我只是将文本字段包装在 UIComponent 中以将其放在舞台上,但使用自动调整大小似乎工作正常:
I don't have edit priv on other people's post's so I'm posting this as a separate answer but credit should go to Cameron if this works:
If I see that it does and his is edited I'd delete this one for cleanliness.
Sorry for the garbage post initially was trying to answer the question just did so erroneously... anyhow tested this one and it appears to work. I did this in Flex but you should be able to just use the AS3 part no problem I just wrapped up the textfield in a UIComponent to get it on stage but using the autosize seems to work fine:
这是上面一些代码的更精致的版本。考虑换行符(html 中断和 \n)并通过其他一些优化使创建的 Textfield 对象无效。希望这有帮助。
如果你更喜欢在课堂上这样做,请在我的 GIT 框架中查看:
https://github.com/charlesclements /as3-tools/blob/master/net/charlesclements/util/text/TextUtil.as
AS3-tools:https://github.com/charlesclements/as3-tools
另外,我们的 Flash/JS 兄弟 Jack Doyle @ GreenSock 有一些与操作文本有关的方便的东西。非常值得一看:http://www.greensock.com/splittext/
This is a more polished version of some of the above code. Accounting for linebreaks (html break and \n) and nullifying the created Textfield object with some other optimizations. Hope this is helpful.
If you prefer this in a class, check it out in my GIT framework:
https://github.com/charlesclements/as3-tools/blob/master/net/charlesclements/util/text/TextUtil.as
AS3-tools:https://github.com/charlesclements/as3-tools
Also, our Flash/JS brother Jack Doyle @ GreenSock has some handy stuff to do with manipulating text. Well worth it to check it out:http://www.greensock.com/splittext/
这是我为 Java 编写的例程。我想你可以将它转换为 Javascript 或其他什么。
它并不准确,但给出了一个大概的答案。假设一个空间的宽度约为200,M和W的宽度约为1000。显然,您需要根据您的特定字体大小进行缩放。
Here is a routine I wrote for Java. I guess you can convert it to Javascript or whatever.
It is not exact, but gives an approximate answer. It assumes that a space is about 200 wide, and M and W is about 1000 wide. Obviously you'll need to scale that based on your particular font size.