Flex:对 mx:Label 或 mx:Text 中选定的文本执行*任何操作*?

发布于 2024-07-24 07:22:16 字数 757 浏览 5 评论 0原文

Flex 的文本编辑控件(mx:TextField、mx:TextArea)提供了对所选文本进行“操作”的功能(selectionBeginIndexselectionEndIndexsetSelection code>),但文本显示控件(mx:Label、mx:Text)似乎没有提供任何此类功能。

经过一番修补后,我尝试对 Label 进行子类化,然后编写可以访问底层 TextField 实例的函数...但即便如此,设置选择也不起作用!

function get selectionBeginIndex():int {
    // This works
    return this.textField.selectionBeginIndex;
}

function get selectionEndIndex():int {
    // This works
    return this.textField.selectionEndIndex;
}

function setSelection(beginIndex:int, endIndex:int):void {
    // But this has no effect!
    this.textField.setSelection(beginIndex, endIndex);
}

那么,是否有更好的方法来访问/更改 mx:Label 和 mx:Text 控件中选择的文本? 是否有“更好”的控件可供使用?

Flex's text-editing controls (mx:TextField, mx:TextArea) offer functions for doing "stuff" with the selected text (selectionBeginIndex, selectionEndIndex, setSelection), but the text-displaying controls (mx:Label, mx:Text) don't seem to offer anything of the sort.

After some tinkering, I've tried subclassing Label then writing functions which give access to the underlying TextField instance... But even then, setting the selection didn't work!

function get selectionBeginIndex():int {
    // This works
    return this.textField.selectionBeginIndex;
}

function get selectionEndIndex():int {
    // This works
    return this.textField.selectionEndIndex;
}

function setSelection(beginIndex:int, endIndex:int):void {
    // But this has no effect!
    this.textField.setSelection(beginIndex, endIndex);
}

So, is there any better way to access/change the text which is selected in mx:Label and mx:Text controls? Are there "better" controls to use?

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

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

发布评论

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

评论(2

娇女薄笑 2024-07-31 07:22:16

您可能最好使用可编辑属性设置为 false 且 borderStyle 设置为“none”的 TextInput 而不是 Label。

对于 Text 组件,在某些情况下它比 Label 好一点,因为它允许用户用鼠标选择显示的文本,并且支持多行。 但我不知道你是否可以以编程方式选择文本,尽管我怀疑如果你对它进行子类化,你会比对标签进行子类化更成功。 或者,您可以使用具有我为 TextInput 提到的相同属性的 TextArea。

要使 TextInput 根据文本调整大小,您可以重写 validateProperties 方法,并使用 getLineMetrics() 计算所需的宽度:

    public override function validateProperties():void
    {
        super.validateProperties();
        var tm : TextLineMetrics = this.getLineMetrics(0);
        var txtWidth : uint = tm.width + tm.leading + 10;
        this.width = minWidth < txtWidth ? txtWidth : minWidth;
    }

可能需要根据您可能使用的字体更改“10”值。
如果您也需要在键入时调整 TextInput 的大小,则需要侦听其更改事件并从处理程序调用 validateProperties。

You might be better off using a TextInput with editable property set to false, and borderStyle set to "none" instead of a Label.

Concerning the Text component, that's a little better than Label in certain situations, because it allows the user to select the displayed text with the mouse, and supports multiple lines. But I don't know if you can select the text programmaticaly, though I suspect if you subclassed it, you'd have more success with it than with subclassing Label. Alternatively you could use a TextArea with the same properties I mentioned for TextInput.

To make the TextInput resize based on his text you can override the validateProperties method, and use getLineMetrics() to calculate the needed width:

    public override function validateProperties():void
    {
        super.validateProperties();
        var tm : TextLineMetrics = this.getLineMetrics(0);
        var txtWidth : uint = tm.width + tm.leading + 10;
        this.width = minWidth < txtWidth ? txtWidth : minWidth;
    }

That "10" value might be needed to change based on the font you use probably.
If you need the TextInput to resize while you type too, you need to listen to its change event and call validateProperties from the handler.

断念 2024-07-31 07:22:16

您可能想要使用 UITextField,即 flash TextField 的 Flex 版本。 这是在 TextArea 内用于显示文本的对象。 它支持您正在寻找的那些索引。

请注意,样式有点奇怪。 “setStyle”什么也不做。 并且您必须重写 validateNow 函数才能使用 setTextFormat,否则您的设置将被清除。 我不知道如何使用 CSS,但这在当时并不是一个问题。

You might want to use UITextField, the flex version of the flash TextField. This is the object used inside a TextArea for displaying the text. It supports those indexes that you're looking for.

Be warned that styling is a bit weird. "setStyle" does nothing. And you have to override the validateNow function to use the setTextFormat, otherwise, your settings get wiped out. I'm not sure how to use CSS with it, but that wasn't a concern at the time.

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