AS3 : How to show image within text in same line - actionscript-3

I would like to show some images and text in the same line. The text and image is completely dynamic. At first I thought to use htmlText property of TextField as img tag is supported. Something like this:
var tf:TextField = new TextField();
tf.htmlText = "before img <img src='img.png'> after image";
addChild(tf);
But I found that the image is showing in the next line, not in the middle of two text segments. Then after checking the manual of TextField I have found that this is the documented behavior that the image will be shown on the line following the img tag.
So what is the best way to do this? Note that the text and position of images are completely dynamic.
I understand that it is possible to split the input in a series of texts and images and then position them calculating the text and images width. In other words parse the input ourselves.
Is there any better way to do this? Or is there any library available that does this parsing?

Hail, Taskinoor.
You need use TLF.
You can follow this link that describe even more that you want.
Edit: I'm assuming you are using Flex (TextFlow).
Don't know if it is possible directly on Flash with Flex SDK4...
Maybe you'll need TLF2 (Flex SDK 4.5 Hero)

Related

Setting QTextDocument painter's rectangle (where to paint)

I am painting on a window simple html using QTextDocument::drawContents(painter)
I want to do the drawing inside some margins in the window but I don't see a direct way to specify the target rectangle of the painting (in the painter/window).
I guess a few ways to do it:
Using the QTextDocuments::setMargin (although this does not allow different values for left/top.
Placing the html into an styled <div>
Applying a translation transform to the painter.
But all this seems a bit too much for what I want to do and I guess if I a missing something straight (as you do with QPainter::drawText where you tell the target rectangle)
Set the textWidth property to the width of the area where the text is supposed to fit. The clipping rectangle you pass to drawContents will cut the text off vertically if there's too much of it to fit; you can't do much about that of course.
So, this would be the missing function you're after:
void drawContents(QPainter * p, QTextDocument & doc, const QRectF & rect) {
p->save();
p->translate(rect.topLeft());
doc.setTextWidth(rect.width());
doc.drawContents(p, rect);
p->restore();
}
Yes, you do need to jump through a few hoops, that's why it needs to be factored out. It's perhaps lamentable that a similar overload of drawContents doesn't exist.

Making dynamic text movable?

I have this DIY designer where a user can put text on a board. The problem is it cant be moved. I can do draggable movieclips but not dynamic text. Is it possible?
I'm assuming your dynamic text is going into something like a label or an Flex TextArea or TextInput or something. You should just be able to move the container itself by setting the x and y properties. TextInputs have opaque backgrounds, but it sounds like you should probably be using a Flex TextArea or something similar anyway.

how to change text vertical offset in textfield as3

Hello.
Is there any way to discover how big is vertical offset in text field(red line, place beetween begining of text field and start of text). Can I also change it ?
Thank you for advice
There is no way to measure this distance. Maybe TextLineMetrix can help you little. Check this document:
textLineMetrics reference
Also you can use textHeight property of text field.
you can trace a comparison between the property textHeight and textField.height that may tell you the difference.
if you want to adjust it manually a work around may be in order such as creating a Sprite that holds the graphics of your box, or border like you have displayed, then imposing a textField on that which you can place by its coordinates.

TextField autoSize+italics cuts of last character

In actionscript 3, my TextField has :
CSS styling
embedded fonts
textAlign : CENTER
autoSize : CENTER
... when italics are used the very right character gets slightly cut off (specially caps).
It basically seems that it fails detecting the right size.
I've had this problem before but just wondered is there a nice workaround (instead of checking textWidth or offsetting text etc.)?
Initialize your textField as you always do, using multiline, autosize, htmlText...
Then do this little trick :
// saving wanted width and height plus 1px to get some space for last char
var savedWidth = myTextField.width + 1;
var savedHeight = myTextField.height + 1;
// removing autoSize, wich is the origin of the problem i think
myTextField.autoSize = "none";
// now manually autoSizing the textField with saved values
myTextField.width = savedWidth;
myTextField.height = savedHeight;
Not that it is much comfort to you, but Flash sometimes has trouble with this seemingly simple task. CSS styling of html TextField was a nice addition but it has caused headaches for text-rendering. In fact I very rarely use CSS for styling text for that reason. I can only imagine that combining bold, italic and normal type faces within the HTML causes Flash to get some of the width calculations wrong which causes autoSize to set the mask a tiny bit short. I hope very much that the new text rendering engine in Flash Player 10 will finally fix these issues (it certainly looks better in theory).
So my solution is never to use HTML with the exception being when I require <a> links in my text ... and there are even some tricky text shifting issues there. In those cases I avoid mixing different font weights and font styles within the same text field. All other cases I use TextFormat directly on TextField.
I suppose if you can't get out of your current architecture (for some reason) you could try adding to the end of your html encoded strings. Or you could manually set the width of the field and not rely on autoSize (as you have mentioned). But if you keep on the CSS/HTML route you may find another new and painful limitation just when you don't want it.
I've had issues with TextField masks behaving differently in the Flash preview, and in the actual browser plugin. Usually, and this is strange to me, it would appear more correctly in the browser. Have you tried running the swf in a browser to see if the problem is actually an annoyance rather than a permanent problem?
I had said this:
My in-ideal approach to solving this is to attach a change event to the TextField which always adds a space after the last character of the field. And then to remember to trim this space off when using the value.
But that didn't take into account that this probably doesn't have a change event and that it's an HTML rendered text field. To add a trailing space in the HTML text field throw in an again, that's not really fixing the problem.

AS3 AutoSize Textfield

in AS3, I'm making this dynamic textfield that is populated from an XML file based on where the user clicks.
The dynamic textfield has a custom scrollbar to it. My problem is that if the text inserted into the textfield is less than the previously displayed text, you can still scroll the dynamic textfield as far as the previous one.
Is there a way to reset the textfield autoSize?
Many thanks,
If you are talking about Flash ( I have very little experience in Flex ) then the textField.autosize property would probably get you in the right direction.
I think this link will tell you exactly what to do.
Are you using any of the standard flex controls? Or, is this flash?
You can set the width of the field to the string length + some space (by binding the width to the string's length/event handling etc). This may be of interest. Of course,
Try this, wrap the xml elements text in
<text><![CDATA[<span class="someClass">Some Text Here</span>]]></text>
If the prior wasn't wrapped in CDATA, flash would have taken the <span class="someClass"> and attempted to drop it down a line and indent it as you would with XML hierarchy; CDATA tells flash to ignore those characters and literally treat them as a string. The result of not using this process is that there are many odd spacings in your textfield.
Also be sure that your TextField is set to Multiline and wordwrap, as well as autoSize such as TextFieldAutoSize.LEFT;