2011-10-11 88 views
1

我在Flex中制作RTE,并试图制作文本格式按钮。Flex - 在TextArea中切换粗体文本

<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(); 
     } 

当我键入一些文本到文本区域,然后选择它,然后单击boldBtn我得到一个无法访问空对象引用的属性或methof。如果我注释掉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); 
} 

有什么不对的代码?

+1

您收到空错误。不会特别崩溃,但这意味着代码有问题。很可能,getFormatOfRange返回null。 –

+0

我刚做了一个编辑,这段代码看起来不错,但不起作用 – RapsFan1981

+0

你是什么意思“看起来很好”和“不工作”?看起来在那里没有空检查。另外,什么是mainTextField?你的代码的其余部分在哪里? –

回答

0

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

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