567 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Ireland
  • Referred between 10 and 49 users
  • Sold between 10 000 and 50 000 dollars
iamdok says

Hi, There have been a few posts about the frustrations of loading html text containing images and text into a dynamic Textfield (Flash Player 9), so in solving my own problems I thought I’d share the results/methods I’ve used. Thanks to all those who had input.

The goal is to:

  • Get the overall height of the textfield into which the htmlText including images is loading.
  • Format the text and images nicely to avoid text flowing around the images.

This assumes the following:

  • That you have no access to the htmlText.
  • There is no id attribute for the img tags in the htmlText
  • There is no height attribute for the img tags in the htmlText.

Sequence:

  1. Find the number of img tags in the htmlText. For this I have used the string utilities class by grant skinner. CrackerJack mentioned it in a previous thread but here is the link again:

    String Utilities Class

    Specifically I have used the “countOf” methos to return the number of image tags.

  2. Strip out the img paths and place into an array for loading manually. This can be done using this function: http://pastie.org/970888
  3. Load the images one by one, listening for the “INIT” event rather than the “COMPLETE” event. Once this event fires, the height of the image will be available to be read. Add the height to a heights Array and abort the rest of the load. Then move to the next image.
  4. Add a height attribute to each img tag in the htmlText. This step is required for the next step to work properly. This can be done using the following static class, writted by the awesome CrackerJack to insert a break after each img tag, but slightly altered by me to insert the height attribute also. http://pastie.org/970881 Pass the heights array to the constructor and it will add the correct height attribute to each image.
  5. Assign the original htmlText to the textField (with autosize applied) and immediately record the height (before the imgs have loaded). Once the height has been recorded, the html Text can be removed from the textField. Get the sum of the heights in the heights array and add it to this textField height. This will be the final height of the textfield. (may need to be adjusted slightly to account for a line break for each image).
  6. Next, we want to format the images and the text nicely so we don’t have any flowing text. To do this we use the following open source class, developed by a guy named Paul Klinkenberg.(see licence notice in class for full details). http://pastie.org/970882 This class requires that each image has a height attribute to work.(hence our use of CJ’s class above). This class returnes an altered htmlText string, which is then assigned to our textField.
  7. If you are using css to style any hyperlinks in the text then you will probably need to include the following step to avoid the text jumping around on rollover of those links.

    We already have the overall height of the final textfield so we can now remove the Autosizeing of the textfield and just assign this height directly. By doing this we get rid of the jumping text effect completely.

And thats it. Now all images will be in a block of their own, with a break before and after the images, and all text beginning on a new line.

Obviously this is not the quickest little setup, but if there are not too many images in the html, and the images are small in filesize, its not as long as it sounds.

One things to watch is that the code doesn’t check if a height attribute already exists on the img tags before inserting one.

Hope this helps someone out.

Donagh.

1161 posts
  • Author had a File in an Envato Bundle
  • Has been a member for 5-6 years
  • Sold between 50 000 and 100 000 dollars
  • Exclusive Author
  • Referred between 10 and 49 users
  • Bought between 50 and 99 items
FlashTang says

Good job iamdok :)

5310 posts The Dude Abides
  • United States
  • Exclusive Author
  • Has been a member for 5-6 years
  • Elite Author
  • Bought between 100 and 499 items
  • Referred between 100 and 199 users
  • Author had a Free File of the Month
+5 more
CodingJack says

Bravo man! This page is bookmarked :)

8186 posts Community Moderator
  • Attended a Community Meetup
  • Community Moderator
  • Has been a member for 6-7 years
  • United Kingdom
  • Contributed a Tutorial to a Tuts+ Site
  • Won a Competition
  • Contributed a Blog Post
  • Beta Tester
  • Bought between 50 and 99 items
+4 more
MSFX Volunteer moderator says

hats off, bookmarked. You got a working demo as well? :)

4140 posts
  • Bought between 1 and 9 items
  • Brazil
  • Exclusive Author
  • Has been a member for 6-7 years
  • Referred between 1 and 9 users
  • Sold between 5 000 and 10 000 dollars
tsafi says
by
by
by
by
by
by