5 Replies Latest reply on Mar 20, 2007 9:45 AM by mduey

    htmlText

    richardbrenkley
      Anyone know how to render dynamic html text? Seems the documentation suggests using:
      <mx:Text id="myTextWrapper">
      <mx:htmlText>

      and then wrapping the html content in <![CDATA[]]> but that doesn't work if the HTML that needs to be rendered is coming from a data source.

      HELP!
        • 1. Re: htmlText
          peterent Level 2
          Just do: myTextWrapper.htmlText = variable_that_contains_the_HTML_text;

          For example:

          var test:String = "This is <b>a test</b><br>And a new line here";
          myTextWrapper = test;

          or
          [Bindable] private var test:String; // set from some data source
          ...
          <mx:Text htmlText="{test}" ... />

          The latter uses data binding. When the result from your data source changes the test variable, Flex will update the Text component's htmlText property.
          • 2. Re: htmlText
            mduey
            I tried this and it works if there is no html markup in the source string. However, if the is any html (<b>mytext</b> for example) then [object Object] is displayed. If I wrap the source with <![CDATA[]]>, then it works OK, but I don't want to include that wrapper on every entry in my xml file. If I try to wrap the <![CDATA[]]> programatically

            var myText:String = " <![CDATA[" + myvariable + "]]>";

            I still get errors. Any ideas?
            • 3. Re: htmlText
              ntsiii Level 3
              Here is a simple example:
              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical"
              creationComplete="initApp()">
              <mx:Script><![CDATA[

              [Bindable]private var _sHtmlText:String;
              private function initApp(): void
              {
              _sHtmlText = "this is a <b>bold</b> word and <i>this is italic</i>";
              }//

              ]]></mx:Script>
              <mx:Label htmlText="{_sHtmlText}" />
              </mx:Application>

              Tracy
              • 4. Re: htmlText
                mduey Level 1
                Thanks Tracy....that works great, however, I get the error when I use text from an xml file:

                <?xml version="1.0" encoding="utf-8"?>
                <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="myData.send()">
                <mx:Script><![CDATA[

                [Bindable]private var snippets:ArrayCollection=new ArrayCollection();

                private function rpcData(event:ResultEvent):void{
                snippets=event.result.textblocks.item;
                }

                private function myText_initialize(textbit:String):void {
                myText.htmlText = textbit;
                }

                ]]></mx:Script>

                <mx:HTTPService id="myData" url="snippets.xml" result="rpcData(event)"/>

                <mx:Text id="myText" initialize="myText_initialize(snippets.getItemAt(0).content)" />

                </mx:Application>

                If the text retrieved from the xml file has no html markup (for example, "This is the text"), it works fine. If there is html markup in it (for example, "This the the <b>bold</b> text"), then I get [object Object] displayed instead of the text.
                If I wrap the text in the xml file (for example, "<![CDATA[ This is the <b>bold</b> text ]]>") it works fine, but I don't want to put that wrapper on every line in the xml file.
                I have also tried putting the wrapper on in the function (see my post above) and that does not work either.
                TIA
                • 5. Re: htmlText
                  mduey Level 1
                  OK....I got it to work. I had to grab the xml data as XML (not as an ArrayCollection). Here it is again with the corrections made:

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="myData.send()">

                  <mx:Script><![CDATA[
                  import mx.rpc.events.ResultEvent

                  [Bindable]
                  private var snippets:XML;

                  private function rpcData(event:ResultEvent):void{
                  snippets=event.result as XML;
                  }

                  ]]></mx:Script>

                  <mx:HTTPService id="myData" url="snippets.xml" resultFormat="e4x" result="rpcData(event)"/>

                  <mx:Text htmlText="{snippets.item[0].title}" condenseWhite="true" />
                  <mx:Text htmlText="{snippets.item[0].content}" condenseWhite="true" />

                  </mx:Application>

                  Now, I can use some html formatting in my xml file, for example:
                  <item>
                  <title>This is the <b>TITLE</b></title>
                  <content><p>This is the first paragraph</p><p>This is the second paragraph</p></content>
                  </item>