/ Published in: ActionScript
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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
var bits:Array = new Array(); function addHtmlTag(tf:MovieClip,indexStart:Number,indexEnd:Number,tag:String):String{ bits = new Array(); bits = getStringParts(tf,indexStart,indexEnd); if(bits != undefined){ var newString:String = bits[0] + " <" + tag + ">" + bits[1] + " </" + tag + ">" + bits[2]; return newString; } } function getStringParts(tf:MovieClip,indexStart:Number,indexEnd:Number):Array{ var wholeString:String = tf.htmlText; var totalIndex:Number = wholeString.length; bits.push(wholeString.substring(0,indexStart)); bits.push(wholeString.substring(indexStart,indexEnd)); bits.push(wholeString.substring(indexEnd,totalIndex)); //trace("bits: "+bits); return bits; } // usage // var newText:String = addHtmlTag(myTextField_mc, 0, 10, "b");