add HTML tag to a string


/ Published in: ActionScript
Save to your folder(s)

This is a handy little function that takes the htmlText value of a TextField and ads a specified tag at the beginning and end of the string. Handy for quick formatting tags such as bold, italic, underline, but could be used to add complex style data as it takes any string value.


Copy this code and paste it in your HTML
  1. var bits:Array = new Array();
  2.  
  3. function addHtmlTag(tf:MovieClip,indexStart:Number,indexEnd:Number,tag:String):String{
  4. bits = new Array();
  5. bits = getStringParts(tf,indexStart,indexEnd);
  6. if(bits != undefined){
  7. var newString:String = bits[0] + " <" + tag + ">" + bits[1] + " </" + tag + ">" + bits[2];
  8. return newString;
  9. }
  10. }
  11.  
  12. function getStringParts(tf:MovieClip,indexStart:Number,indexEnd:Number):Array{
  13. var wholeString:String = tf.htmlText;
  14. var totalIndex:Number = wholeString.length;
  15. bits.push(wholeString.substring(0,indexStart));
  16. bits.push(wholeString.substring(indexStart,indexEnd));
  17. bits.push(wholeString.substring(indexEnd,totalIndex));
  18. //trace("bits: "+bits);
  19. return bits;
  20. }
  21.  
  22. // usage
  23. // var newText:String = addHtmlTag(myTextField_mc, 0, 10, "b");

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.