سؤال

I made selected text in text area bold,italic etc but i am not able to maintain bold ,italic properties when adding text on rich text.

هل كانت مفيدة؟

المحلول

Ok, look. Let's analyze our problem step by step:

Problem: we have text and we need to change selected text of initial text to bold;

Solution:

1) Take a selection text to separate string

2) Change style of separate string to bold(or other style)

3) Collect initial not-bold strings with bolded sting and show result :)

I think there are several ways to resolve this, but i propose the following:

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" title="Test">
<s:VGroup>
    <s:RichEditableText id="ret" text="Test Text String for selection" />
    <s:Button id="btnBold" label="Set Bold for Selection" click="btnBold_clickHandler(event)"/>
</s:VGroup>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<fx:Script>
    <![CDATA[
        import flashx.textLayout.conversion.TextConverter;

        protected function btnBold_clickHandler(event:MouseEvent):void
        {
            setBoldForSelectionTex();
        }

        private function setBoldForSelectionTex():void
        {
            var startSelectionPos:int   = 0;
            var endSelectionPos:int     = 0;

            if(ret.selectionActivePosition > ret.selectionAnchorPosition)
            {
                endSelectionPos     = ret.selectionActivePosition;
                startSelectionPos   = ret.selectionAnchorPosition;
            }
            else
            {
                endSelectionPos     = ret.selectionAnchorPosition;
                startSelectionPos   = ret.selectionActivePosition;
            }

            var initialString:String    = ret.text;
            var startString:String      = ret.text.toString().substr(0, startSelectionPos);
            var middleString:String     = ret.text.toString().substr(startSelectionPos, endSelectionPos - startSelectionPos);
            var endString:String        = ret.text.toString().substr(endSelectionPos, initialString.length - endSelectionPos);
            var completeString:String   = startString +"<b>" + middleString + "</b>" + endString;

            ret.textFlow = TextConverter.importToFlow(completeString, TextConverter.TEXT_FIELD_HTML_FORMAT);
        }
    ]]>
</fx:Script>
</s:View>

I hope this help you.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top