Wim Deblauwe
Wim Deblauwe

Reputation: 26858

Change text selection color for TextArea in Flex 4 for programmatic selection

I am using a Flex 4 spark TextArea and I implemented a search field. I use the following code to highlight the searched text in the TextArea:

private function findAndHighlightText():void
    {
        var text:String = findTextInput.text;
        var beginIndex:int = scriptSourceTextArea.text.indexOf( text, m_lastFoundIndex );
        if (beginIndex == -1 && m_lastFoundIndex > 0)
        {
            // We are at the end, search back from the begin
            Alert.show( resourceManager.getString( 'groovyresources', 'search.at.end' ),
                        resourceManager.getString( 'groovyresources', 'find' ) );
            m_lastFoundIndex = 0;
            beginIndex = scriptSourceTextArea.text.indexOf( text, m_lastFoundIndex );
        }
        if (beginIndex != -1)
        {
            var endIndex:int = beginIndex + text.length;
            m_lastFoundIndex = endIndex;
            scriptSourceTextArea.selectRange( beginIndex, endIndex );
            scriptSourceTextArea.scrollToRange( beginIndex );
        }
        else
        {
            Alert.show( resourceManager.getString( 'groovyresources', 'search.not.found', [text] ),
                        resourceManager.getString( 'groovyresources', 'find' ) );
        }
    }

The most important is the method selectRange on the TextArea. This highlights the text in the TextArea, but I would like to use a different color.

I can change the highlight color for manual selection by applying the CSS style focused-text-selection-color (See http://www.kirupa.com/forum/showthread.php?354479-Change-highlight-color-for-text-fields), but this does not change the color for the programmatic selection.

UPDATE: The color does not change for the programmatic selection because the TextArea does not have the focus at that moment. So I need to find the unfocused selection color CSS name.

Upvotes: 0

Views: 608

Answers (1)

Wim Deblauwe
Wim Deblauwe

Reputation: 26858

There are 2 styles that influence how the selected text is highlighted in Flex:

    .scriptSourceTextArea {
        focused-text-selection-color: #788ec5;
        unfocused-text-selection-color: #7e94cb;
    }

One is for focused, other is for when the text area does not have the focus.

Upvotes: 1

Related Questions