7

我已经对这个主题进行了很多搜索,但似乎我发现的内容要么已过时,要么似乎不起作用。

过去使用 TextFields,您可以将 TextField 设置为某个宽度,将 wordWrap 设置为 true,您最终会得到一个根据您添加的文本更改高度的文本字段。

现在我正在尝试使用 Spark TextArea 或 RichText 来执行此操作。

我试过这个 HeightInLines = NAN,但这似乎已经过时了。

我也试过这个程序:

var totalHeight:uint = 10;
this.validateNow();
var noOfLines:int = this.mx_internal::getTextField().numLines;
for (var i:int = 0; i < noOfLines; i++) 
{
     var textLineHeight:int = 
                     this.mx_internal::getTextField().getLineMetrics(i).height;
     totalHeight += textLineHeight;
}
this.height = totalHeight;

但是 mx_internal 不在 Spark 组件中。

我正在尝试使用 AS3,而不是 MXML 来做到这一点。如果有人有任何建议或链接可以帮助我使用 AS3 解决这个问题,我将不胜感激。

4

7 回答 7

3

整个下午都在为此苦苦挣扎。但是,如果您设置它的宽度并保持它的高度未定义,它看起来 RichEditableText 组件将自动调整大小。

于 2011-10-26T22:03:17.837 回答
2

这工作正常:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <s:TextArea updateComplete="event.currentTarget.heightInLines = NaN" />
</s:Application>

在这里的评论中找到。您可以使用相同的updateComplete事件在 ActionScript 中执行相同的操作。

于 2011-04-15T18:22:56.813 回答
1

您可以从 TextArea 的皮肤中移除滚动条,它会变为可自动调整大小。您可以在这里下载完成的皮肤:http ://www.yumasoft.com/node/126

于 2012-04-09T07:31:20.493 回答
1

This is how I set the height of a TextArea to fit its content when used inside an ItemRenderer (e.g. for a List component):

private function onUpdateComplete( e: Event ): void
{
    // autoresize the text area
    if ( theText ) {
      var actualNumOfLines: int = theText.textFlow.flowComposer.numLines;
      theText.heightInLines = actualNumOfLines; 

      invalidateSize();
    }
}

ItemRenderer must have this property set:

<s:ItemRenderer ... updateComplete="onUpdateComplete(event)>

Maybe the updateComplete event is not the optimal trigger for auto-resize actions but works fine for me.

于 2012-02-08T20:26:45.087 回答
0

这是火花文本区域的解决方案(它与 mx 文本区域一样):

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

这似乎对我有用:

<s:TextArea id="testTextArea"
            change="testTextArea_changeHandler(event)" 
            updateComplete="testTextArea_updateCompleteHandler(event)"/>

<fx:Script>
    <![CDATA[
        protected function testTextArea_changeHandler(event:TextOperationEvent):void
        {
            testTextArea.height = RichEditableText(testTextArea.textDisplay).contentHeight + 2;
        }

        protected function testTextArea_updateCompleteHandler(event:FlexEvent):void
        {
            testTextArea.height = RichEditableText(testTextArea.textDisplay).contentHeight + 2;
        }
    ]]>
</fx:Script>
于 2016-06-29T09:34:52.413 回答
-1

一直在敲打 s:TextArea,然后发现这可以完成工作:

<s:RichEditableText id="txtArea" left="0" right="0" backgroundColor="#F7F2F2"
                    change="textChanged()" />
于 2011-12-28T13:52:37.830 回答