2012-12-21 59 views
0

如何防止在雙擊Flex 4 TextArea中選擇文本時包含句點?例如,在Flex 4 TextArea組件中,如果我有單詞「something.else」,並雙擊單詞「something」,它將突出顯示「something.else」。如何防止在雙擊Flex 4 TextArea中選擇文本時包含句號?

如何讓它只突出顯示「某物」?

我已經用google搜索了這個API,挖了API,查了相關的類,還沒有發現任何「官方」的東西。

注意:我寧願避免簡單地攔截雙擊事件來處理設置選擇範圍,因爲恢復其他現有功能的複雜性可能變得無法克服。

回答

2

1) TextArea使用RichEditableText。 RichEditableText使用SelectionManager。 SelectionManager具有公共函數mouseDoubleClickHandler。 mouseDoubleClickHandler使用ParagraphElement(它是final類)及其findPreviousWordBoundary和findNextWordBoundary函數。 他們使用TextBlock的功能和相似的名字。而TextBlock是playerglobal.swc的一部分。

因此,通過覆蓋SelectionManager的處理程序,然後擴展RichEditableText和一些基礎結構類以使其使用您的自定義SelectionManager,可以應用覆蓋方法的唯一「專業」方式。

但這不是一個簡單的方法。

2)您可以在'。'周圍放置一個空格字符。 (更喜歡狹窄的字體或創建自定義字體,其中一個字符的寬度爲0)。

rslt[0x0020] = true; //SPACE 
      rslt[0x1680] = true; //OGHAM SPACE MARK 
      rslt[0x180E] = true; //MONGOLIAN VOWEL SEPARATOR 
      rslt[0x2000] = true; //EN QUAD 
      rslt[0x2001] = true; //EM QUAD 
      rslt[0x2002] = true; //EN SPACE 
      rslt[0x2003] = true; //EM SPACE 
      rslt[0x2004] = true; //THREE-PER-EM SPACE 
      rslt[0x2005] = true; //FOUR-PER-EM SPACE 
      rslt[0x2006] = true; //SIZE-PER-EM SPACE 
      rslt[0x2007] = true; //FIGURE SPACE 
      rslt[0x2008] = true; //PUNCTUATION SPACE 
      rslt[0x2009] = true; //THIN SPACE 
      rslt[0x200A] = true; //HAIR SPACE 
      rslt[0x202F] = true; //NARROW NO-BREAK SPACE 
      rslt[0x205F] = true; //MEDIUM MATHEMATICAL SPACE 
      rslt[0x3000] = true; //IDEOGRAPHIC SPACE 
      //members of LineSeparator category 
      rslt[0x2028] = true; //LINE SEPARATOR 
      //members of ParagraphSeparator category 
      rslt[0x2029] = true; 
      //Other characters considered to be a space 
      rslt[0x0009] = true; //CHARACTER TABULATION 
      rslt[0x000A] = true; //LINE FEED 
      rslt[0x000B] = true; //LINE TABULATION 
      rslt[0x000C] = true; //FORM FEED 
      rslt[0x000D] = true; //CARRIAGE RETURN 
      rslt[0x0085] = true; //NEXT LINE 
      rslt[0x00A0] = true; //NO-BREAK SPACE 

3)所以,我認爲你的選擇與自定義doubleclick處理程序沒有那麼差。

+0

哇靠,這是信息。這給了我很多想法,感謝你在這裏徹底的:) –

1

這裏是我去解決:

protected var lastClickedSelectionAnchorIndex:int = 0; 

protected function textArea_doubleClickHandler(event:MouseEvent):void 
{ 

    if(event.target != textArea.textDisplay) return; 

    if(textArea.selectionAnchorPosition == textArea.selectionActivePosition) return; 

    var targetText:String = textArea.text; 

    if(targetText == null || targetText == "" || targetText.indexOf(".") === -1) return; 

    var selectedText:String = targetText.substring(textArea.selectionAnchorPosition, textArea.selectionActivePosition); 

    if(selectedText == null || selectedText == "" || selectedText.indexOf(".") === -1) return; 

    var selectionStart:int = textArea.selectionAnchorPosition; 
    var selectionEnd:int = textArea.selectionActivePosition; 

    if(selectionStart > selectionEnd){ 

     selectionStart = textArea.selectionActivePosition; 
     selectionEnd = textArea.selectionAnchorPosition; 

    } 

    if(lastClickedSelectionAnchorIndex < selectionStart || lastClickedSelectionAnchorIndex > selectionEnd){ 

     return; 

    } 

    var newSelectionStart:int = targetText.lastIndexOf(".", lastClickedSelectionAnchorIndex); 
    var newSelectionEnd:int = targetText.indexOf(".", lastClickedSelectionAnchorIndex); 

    var startSelectionOffset:int = 1; 

    if(newSelectionStart < selectionStart || newSelectionStart === -1){ 

     newSelectionStart = selectionStart; 

     startSelectionOffset = 0; 

    } 

    if(newSelectionEnd > selectionEnd || newSelectionEnd === -1){ 

     newSelectionEnd = selectionEnd; 

    } 

    textArea.selectRange(newSelectionStart + startSelectionOffset, newSelectionEnd); 

} 

protected function textArea_mouseDownHandler(event:MouseEvent):void 
{ 

    try{ 

     lastClickedSelectionAnchorIndex = textArea.selectionAnchorPosition; 

    }catch(error:Error){ 

     lastClickedSelectionAnchorIndex = 0; 

    } 

}