Skip to main content
Participating Frequently
April 14, 2011
Answered

<img> not working in RichText

  • April 14, 2011
  • 2 replies
  • 1340 views
I want to 'embed' images in my text and so I started experimenting with RichText, TextFlow, etc. To my frustration, I cannot display images in RichText in the way I would like to create TextFlow object (the way 'string' data would come from database, that is converted to TextFlow by the 'view').

Here is a very simple application to put this point across:

============

<?xml version="1.0" encoding="utf-8"?>
<s:Application
    xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark"
    xmlns:mx="library://ns.adobe.com/flex/mx"
    width="1000"
    creationComplete="application1_initializeHandler(event)"
    >
    <s:RichText
        id="richTxt"
        width="100%"
        textFlow="{textFlow}"
        horizontalCenter="0"
        verticalCenter="0"
        />

    <fx:Declarations>
    </fx:Declarations>
   
    <fx:Script>
        <![CDATA[
            import flashx.textLayout.elements.TextFlow;
            import spark.utils.TextFlowUtil;
           
            import mx.events.FlexEvent;
           
            import org.osmf.layout.AbsoluteLayoutFacet;
           
            [Bindable]
            private var textFlow:TextFlow;
           
            private var inputString:String =
                "<TextFlow whiteSpaceCollapse='preserve' xmlns='http://ns.adobe.com/textLayout/2008'>" +
                "<p>" +
                "<span fontFamily='Verdana' fontSize='18'>There should be an image between this...</span>" +
                "<img height='auto' width='auto' source='assets/images/math.png'/>" +
                "<span fontFamily='Verdana' fontSize='18'>... and this.</span>" +
                "<span></span>" +
                "</p>" +
                "</TextFlow>";
           
            protected function application1_initializeHandler(event:FlexEvent):void {
                textFlow = TextFlowUtil.importFromXML(new XML(inputString));
            }
           
        ]]>
    </fx:Script>
</s:Application>

===========
Here is math.png that you can save in assets/images/

If you replace RichText with TextArea, the image shows up! Could someone please demystify this?
Thanks!
This topic has been closed for replies.
Correct answer Jin-Huang

<img height='auto' width='auto' source='assets/images/math.png'/>

height & width should be given a fixed value. You can try <img height='44' width='174' source='assets/images/math.png'/>.

2 replies

Adobe Employee
April 15, 2011

*<img height='44' width='174' source='http://forums.adobe.com/servlet/JiveServlet/showImage/65075/math.png'/>* works.

Someone once convinced me that it is not a bug and we should input image's size explicitly, because image is loaded asynchronously.

DilipShahAuthor
Participating Frequently
April 15, 2011

Thanks... setting image width and height in insertInlineGraphic method works

just fine.

It's confusing that insertInlineGraphic with image width and height set to

'auto' works for TextArea but doesn't for RichText.

Adobe Employee
April 16, 2011

insertInlineGraphic is a function in textlayout.swc. As far as I know, It needs width and height in pure actionscript projects.

TextArea and RichText are componets in Flex. They may be implemented by different persons. So ...

Jin-HuangCorrect answer
Adobe Employee
April 15, 2011

<img height='auto' width='auto' source='assets/images/math.png'/>

height & width should be given a fixed value. You can try <img height='44' width='174' source='assets/images/math.png'/>.