Length of a string in pixels

前端 未结 4 1122
借酒劲吻你
借酒劲吻你 2020-12-09 21:39

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 th

4条回答
  •  失恋的感觉
    2020-12-09 22:22

    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:

    function measureString(str:String, format:TextFormat):Rectangle {
        var textField:TextField = new TextField();
        textField.autoSize = TextFieldAutoSize.LEFT;
        textField.defaultTextFormat = format;
        textField.text = str;
    
        return new Rectangle(0, 0, textField.textWidth, textField.textHeight);
    }
    

    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:

    
    
        
            
        
        
            
        
        
            
            
        
    
    

提交回复
热议问题