8

我想让我的移动 spark textarea组件包装所有内容。我发现了 mx_internal 这样做的方式,但我无法调用mx_internal::getTextField().numLines方法 - 没有这样的......以前有人做过吗?

4

2 回答 2

3

以下是移动端的解决方案:

for(var i:int=0; i < StyleableTextField(txt_genel.textDisplay).numLines; i++) {
        ta_height += StyleableTextField(txt_genel.textDisplay).getLineMetrics(i).height;
}
txt_genel.height = ta_height;
于 2012-05-21T09:31:32.993 回答
0

这是一个带有少量自定义 TextArea 类的解决方案,有一些注释可以解释更多。

package
{   
    import mx.events.FlexEvent;

import spark.components.TextArea;
import spark.components.supportClasses.StyleableStageText;
import spark.events.TextOperationEvent;

public class CustomTextArea extends TextArea
{

    private var _lineNumber:int = 1;
    private var _padding:int;
    private var _minHeight:int;

    public function CustomTextArea()
    {
        super();

        addEventListener(FlexEvent.CREATION_COMPLETE, function setBehaviour(event:FlexEvent):void
        {
            //minHeight to prevent textarea to be too small
            _minHeight = height;
            //padding between textarea and text component inside to calculate line number
            _padding = ((textDisplay as StyleableStageText).x - x) + (width - (textDisplay as StyleableStageText).width);
            //listener for text changing
            addEventListener(TextOperationEvent.CHANGE, setHeight);
        });
    }

    private function setHeight(event:TextOperationEvent):void
    {
        //line number is textwidth divided by component width
        _lineNumber = (((textDisplay as StyleableStageText).measureText(text).width + _lineNumber * _padding) / width) + 1;
        //text height is line number * text height
        var newHeight:int = _lineNumber * (textDisplay as StyleableStageText).measureText(text).height;
        //if new height > min height, set height
        if (newHeight > _minHeight)
            height = newHeight;
    }
}
}

希望这会有所帮助。

编辑:由于行数很多,TextArea 的高度上升太多。应该管。

于 2012-05-20T14:33:37.803 回答