如何使 mx:textarea 高度与内容相同

发布于 2024-07-17 08:02:34 字数 1625 浏览 4 评论 0原文

文本区域的初始高度比内容大得多,我找不到使其始终与文本内容高度相同的方法:

<mx:TextArea id="textarea" borderStyle="solid" width="100%" wordWrap="true" selectable="false" backgroundAlpha="0" focusAlpha="0" text="this is a little test" />

给出一个比所需高得多的边框框。

如果内容中有链接,这也会带来一个无意的问题,因为当链接不在链接附近时会触发链接“鼠标悬停”。

<mx:Script>
<![CDATA[
public function onInit():void
{
    var style:StyleSheet = new StyleSheet();

    var aLink:Object = new Object();
    aLink.color = "#0000FF";

    var aHover:Object = new Object();
    aHover.color = "#00FF00";
    aHover.textDecoration = "underline";

    style.setStyle( "a:hover", aHover );
    style.setStyle( "a:link", aLink );

    textarea.styleSheet = style;
}
]]>
</mx:Script>


<mx:TextArea id="textarea" width="100%" wordWrap="true" borderStyle="solid" selectable="false" backgroundAlpha="0" focusAlpha="0" >
    <mx:htmlText>
    <![CDATA[<a href='event:http://www.adobe.com'>Navigate to Adobe.com.</a> this is testing nothing at all really]]>
    </mx:htmlText>
</mx:TextArea>

文本组件不会受到此影响,但我无法将样式表附加到文本组件。

希望有人可以提供帮助。 或者我可以使用其他一些组件来添加样式表来设计锚标记的样式。

我发现这个在 TextArea.as 源中是可重写的,如果我重写它并删除“2 x”乘数,它几乎可以工作,但不幸的是,这意味着内容在需要时不会变大,而是垂直滚动,所以它几乎就在那里:

override protected function measure():void
{
    super.measure();

    measuredMinWidth = DEFAULT_MEASURED_MIN_WIDTH;
    measuredWidth = DEFAULT_MEASURED_WIDTH;
    // TextArea is minimum of two lines of text
    measuredMinHeight = measuredHeight = 2 * DEFAULT_MEASURED_MIN_HEIGHT;
}

The initial height of a text area is much larger than the content, I cannot find a way of making it always the same height as the text content:

<mx:TextArea id="textarea" borderStyle="solid" width="100%" wordWrap="true" selectable="false" backgroundAlpha="0" focusAlpha="0" text="this is a little test" />

Gives a bordered box that is much taller than needed.

This also gives an unintential problem if you have links within the content in that a link 'mouseover' is triggered when nowhere near the link.

<mx:Script>
<![CDATA[
public function onInit():void
{
    var style:StyleSheet = new StyleSheet();

    var aLink:Object = new Object();
    aLink.color = "#0000FF";

    var aHover:Object = new Object();
    aHover.color = "#00FF00";
    aHover.textDecoration = "underline";

    style.setStyle( "a:hover", aHover );
    style.setStyle( "a:link", aLink );

    textarea.styleSheet = style;
}
]]>
</mx:Script>


<mx:TextArea id="textarea" width="100%" wordWrap="true" borderStyle="solid" selectable="false" backgroundAlpha="0" focusAlpha="0" >
    <mx:htmlText>
    <![CDATA[<a href='event:http://www.adobe.com'>Navigate to Adobe.com.</a> this is testing nothing at all really]]>
    </mx:htmlText>
</mx:TextArea>

The Text component doesnt suffer from this, but I cannot attach a stylesheet to a text component.

Hoping someone can help. Or is there some other component I can use where I can add a stylesheet to stylise anchor tags.

I found this overridable in the TextArea.as source and if I override it and remove the "2 x" multiplier it almost works but unfortunately it means that the content doesnt get bigger when it needs to and vertically scrolls instead, so its almost there:

override protected function measure():void
{
    super.measure();

    measuredMinWidth = DEFAULT_MEASURED_MIN_WIDTH;
    measuredWidth = DEFAULT_MEASURED_WIDTH;
    // TextArea is minimum of two lines of text
    measuredMinHeight = measuredHeight = 2 * DEFAULT_MEASURED_MIN_HEIGHT;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一花一树开 2024-07-24 08:02:34

如果扩展 Text,则可以添加 getter/setter 来设置底层 UITextField 对象的 styleSheet。

package
{
    import flash.events.Event;
    import flash.text.StyleSheet;

    import mx.controls.Text;

    import mx.core.mx_internal;

    use namespace mx_internal;

    public class StyledText extends Text
    {
        public function StyledText()
        {
            super();
        }

        private var _styleSheet:StyleSheet = null;

        [Bindable("stylesheetChanged")]
        public function get styleSheet():StyleSheet {
            return _styleSheet;
        }

        public function set styleSheet(value:StyleSheet):void {
            _styleSheet = value;

            if ( textField ) {
                textField.styleSheet = _styleSheet;
            }

            dispatchEvent(new Event("stylesheetChanged"));
        }

        override protected function createChildren():void {
            super.createChildren();

            //textField is created in the createChildren 
            //method of the Label class
            if ( textField && styleSheet ) {
                textField.styleSheet = _styleSheet;
            }
        }

    }
}

然后你可以像这样使用该组件:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns1="*" preinitialize="onInit()">
    <mx:Script>
    <![CDATA[
    public function onInit():void
    {
        var style:StyleSheet = new StyleSheet();

        var aLink:Object = new Object();
        aLink.color = "#0000FF";

        var aHover:Object = new Object();
        aHover.color = "#00FF00";
        aHover.textDecoration = "underline";

        style.setStyle( "a:hover", aHover );
        style.setStyle( "a:link", aLink );

        text.styleSheet = style;
    }
    ]]>
    </mx:Script>


    <ns1:StyledText id="text" x="0" y="79">
        <ns1:htmlText>
        <![CDATA[<a href='event:http://www.adobe.com'>Navigate to Adobe.com.</a> this is testing nothing at all really]]>
        </ns1:htmlText>
    </ns1:StyledText>

</mx:Application>

If you extend Text, you can add a getter/setter that allows you to set the styleSheet of the underlying UITextField object.

package
{
    import flash.events.Event;
    import flash.text.StyleSheet;

    import mx.controls.Text;

    import mx.core.mx_internal;

    use namespace mx_internal;

    public class StyledText extends Text
    {
        public function StyledText()
        {
            super();
        }

        private var _styleSheet:StyleSheet = null;

        [Bindable("stylesheetChanged")]
        public function get styleSheet():StyleSheet {
            return _styleSheet;
        }

        public function set styleSheet(value:StyleSheet):void {
            _styleSheet = value;

            if ( textField ) {
                textField.styleSheet = _styleSheet;
            }

            dispatchEvent(new Event("stylesheetChanged"));
        }

        override protected function createChildren():void {
            super.createChildren();

            //textField is created in the createChildren 
            //method of the Label class
            if ( textField && styleSheet ) {
                textField.styleSheet = _styleSheet;
            }
        }

    }
}

Then you can use the component like so:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns1="*" preinitialize="onInit()">
    <mx:Script>
    <![CDATA[
    public function onInit():void
    {
        var style:StyleSheet = new StyleSheet();

        var aLink:Object = new Object();
        aLink.color = "#0000FF";

        var aHover:Object = new Object();
        aHover.color = "#00FF00";
        aHover.textDecoration = "underline";

        style.setStyle( "a:hover", aHover );
        style.setStyle( "a:link", aLink );

        text.styleSheet = style;
    }
    ]]>
    </mx:Script>


    <ns1:StyledText id="text" x="0" y="79">
        <ns1:htmlText>
        <![CDATA[<a href='event:http://www.adobe.com'>Navigate to Adobe.com.</a> this is testing nothing at all really]]>
        </ns1:htmlText>
    </ns1:StyledText>

</mx:Application>
七色彩虹 2024-07-24 08:02:34

我还没有尝试过您正在尝试的操作,但此链接看起来可能会有所帮助:

http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&postId=13628&productId=2

I haven't tried what you're attempting, but this link looks like it might help:

http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&postId=13628&productId=2.

旧人九事 2024-07-24 08:02:34

根据 Bedwyr 的链接,我想我应该尝试自己重新计算高度,这似乎工作正常(尚未注意到任何不良副作用,但可能存在):

this.addEventListener( Event.CHANGE, onReMeasure );
this.addEventListener( Event.RESIZE, onReMeasure );

override protected function measure():void
{
        super.measure();

        measuredMinWidth = DEFAULT_MEASURED_MIN_WIDTH;
        measuredWidth = DEFAULT_MEASURED_WIDTH;

        var lm:TextLineMetrics = getLineMetrics( 0 );
        measuredMinHeight = measuredHeight = DEFAULT_MEASURED_MIN_HEIGHT;
}

private function onReMeasure( eventObj:Event ):void
{
    var ht:Number = 0;
    for( var n:int = 0; n < textField.numLines; n++ )
    {
        ht += textField.getLineMetrics(n).height;
    }
    ht += 10;   // padding

    height = ht;
    validateNow();
}

Based on Bedwyr's link, I thought I would try recalcing the height myself and this seems to work fine (not noticed any bad side-effects yet, but there may be):

this.addEventListener( Event.CHANGE, onReMeasure );
this.addEventListener( Event.RESIZE, onReMeasure );

override protected function measure():void
{
        super.measure();

        measuredMinWidth = DEFAULT_MEASURED_MIN_WIDTH;
        measuredWidth = DEFAULT_MEASURED_WIDTH;

        var lm:TextLineMetrics = getLineMetrics( 0 );
        measuredMinHeight = measuredHeight = DEFAULT_MEASURED_MIN_HEIGHT;
}

private function onReMeasure( eventObj:Event ):void
{
    var ht:Number = 0;
    for( var n:int = 0; n < textField.numLines; n++ )
    {
        ht += textField.getLineMetrics(n).height;
    }
    ht += 10;   // padding

    height = ht;
    validateNow();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文