2011-10-11 86 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"); 
}