Flex - 在 TextArea 中切换粗体文本

发布于 2024-12-09 05:14:04 字数 1466 浏览 2 评论 0原文

我正在 Flex 中制作 RTE,并尝试制作文本格式按钮。

<s:ToggleButton id="boldBtn" width="50" height="50" label="B" click="boldBtn_clickHandler(event)" color="#000000" fontWeight="bold"/>

和我的代码

protected function boldBtn_clickHandler(event:MouseEvent):void
        {
            var txtLayFmt:TextLayoutFormat = mainTextField.getFormatOfRange(null,
                mainTextField.selectionAnchorPosition,
                mainTextField.selectionActivePosition);
            txtLayFmt.fontWeight = (txtLayFmt.fontWeight == FontWeight.BOLD) ? FontWeight.NORMAL : FontWeight.BOLD; **// Causing the NULL Pointer exception**
            mainTextField.setFormatOfRange(txtLayFmt,
                mainTextField.selectionAnchorPosition,
                mainTextField.selectionActivePosition);
            mainTextField.setFocus();
        }

当我在 TextArea 中键入一些文本,然后选择它然后单击粗体Btn时,我得到一个无法访问空对象引用的属性或方法。如果我注释掉 txtLayFmt.fontWeight = (txtLayFmt.fontWeight == FontWeight.BOLD) ? FontWeight.NORMAL : FontWeight.BOLD;该程序不会崩溃,所以这似乎是有问题的行,但我不明白为什么。

编辑 我现在正在尝试这段代码。当我将它放入桌面应用程序中时它可以工作,但是当我将它放入移动项目中时它不起作用。有什么想法吗?

……

private function btnBold_click(evt:MouseEvent):void {
trace("Clicked"); // Traces to output ok
var styleObj:TextLayoutFormat = new TextLayoutFormat();
styleObj.fontWeight = FontWeight.BOLD;
mainTextField.setFormatOfRange(styleObj);
}

这段代码有什么问题吗?

I am making a RTE in Flex and am trying to make text formatting buttons.

<s:ToggleButton id="boldBtn" width="50" height="50" label="B" click="boldBtn_clickHandler(event)" color="#000000" fontWeight="bold"/>

and my code

protected function boldBtn_clickHandler(event:MouseEvent):void
        {
            var txtLayFmt:TextLayoutFormat = mainTextField.getFormatOfRange(null,
                mainTextField.selectionAnchorPosition,
                mainTextField.selectionActivePosition);
            txtLayFmt.fontWeight = (txtLayFmt.fontWeight == FontWeight.BOLD) ? FontWeight.NORMAL : FontWeight.BOLD; **// Causing the NULL Pointer exception**
            mainTextField.setFormatOfRange(txtLayFmt,
                mainTextField.selectionAnchorPosition,
                mainTextField.selectionActivePosition);
            mainTextField.setFocus();
        }

When I type some text into the TextArea, and select it then click the boldBtn I get a Cannot access a property or methof of a null object reference. If I comment out txtLayFmt.fontWeight = (txtLayFmt.fontWeight == FontWeight.BOLD) ? FontWeight.NORMAL : FontWeight.BOLD; the program doesn't crash so this seems to be the offending line but I don't see why.

EDIT
I'm now trying this code. It works when I ut it in a Desktop application, but when I put it in a mobile project it doesn't work. Any ideas?

......

private function btnBold_click(evt:MouseEvent):void {
trace("Clicked"); // Traces to output ok
var styleObj:TextLayoutFormat = new TextLayoutFormat();
styleObj.fontWeight = FontWeight.BOLD;
mainTextField.setFormatOfRange(styleObj);
}

What's wrong with this code?

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

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

发布评论

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

评论(1

唐婉 2024-12-16 05:14:04

将其添加到您的 css 中,它将使用 TLF 并支持所有正常功能。

s|TextArea
{
    skinClass: ClassReference("spark.skins.spark.TextAreaSkin");
}

add this to your css and it will use TLF and support all the normal functionality.

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