Flex - 在 TextArea 中切换粗体文本
我正在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其添加到您的 css 中,它将使用 TLF 并支持所有正常功能。
add this to your css and it will use TLF and support all the normal functionality.