{"id":1136,"date":"2012-07-05T21:38:49","date_gmt":"2012-07-05T20:38:49","guid":{"rendered":"http:\/\/dpscomputing.com\/wordpress\/?p=1136"},"modified":"2012-07-05T21:38:49","modified_gmt":"2012-07-05T20:38:49","slug":"how-to-format-text-in-flash-using-actionscript-3","status":"publish","type":"post","link":"https:\/\/www.dpscomputing.com\/blog\/2012\/07\/05\/how-to-format-text-in-flash-using-actionscript-3\/","title":{"rendered":"How To Format Text In Flash (Using ActionScript 3)"},"content":{"rendered":"<p>Although Flash applications and development has had a mixed press over the past couple of years, it still remains a popular and versatile technology, with the CS6 series of applications released by Adobe recently.<\/p>\n<p>In this &#8216;how-to&#8217; we are going to cover the formatting of text in text fields. \u00a0Now, most, dare I say all Flash developers will be aware of how to format text using the GUI provided in Flash. \u00a0However, what happens if you want to change the formatting of the text during runtime? \u00a0Well, obviously, the fancy GUI can&#8217;t help you out there.<\/p>\n<p>This is where we have to move onto using ActionScript. \u00a0The following examples are in ActionScript 3 &#8211; currently the latest and standard version of ActionScript. \u00a0Again, most Flash developers will know ActionScript and use it on a regular basis, however there are some users who mainly or solely use the GUI so you will probably find this guide most helpful. \u00a0It can also act as a great refresher for the experienced Flash developers.<\/p>\n<p>Obviously we need to start up flash &#8211; here&#8217;s a picture of my blank canvas ready for work to begin!<\/p>\n<p><a href=\"http:\/\/dpscomputing.com\/wordpress\/wp-content\/uploads\/2012\/07\/cs5textformatas3-1.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1138\" data-permalink=\"https:\/\/www.dpscomputing.com\/blog\/2012\/07\/05\/how-to-format-text-in-flash-using-actionscript-3\/cs5textformatas3-1\/\" data-orig-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformatas3-1.png\" data-orig-size=\"1436,839\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"cs5textformatas3-1\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformatas3-1-300x175.png\" data-large-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformatas3-1-1024x598.png\" tabindex=\"0\" role=\"button\" class=\"aligncenter size-large wp-image-1138\" title=\"cs5textformatas3-1\" src=\"http:\/\/dpscomputing.com\/wordpress\/wp-content\/uploads\/2012\/07\/cs5textformatas3-1-1024x598.png\" alt=\"\" width=\"550\" height=\"321\" srcset=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformatas3-1-1024x598.png 1024w, https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformatas3-1-150x87.png 150w, https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformatas3-1-300x175.png 300w, https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformatas3-1.png 1436w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<p>So, firstly we need a text field. \u00a0This can either be created using the GUI or in ActionScript. \u00a0Seen as though we&#8217;re covering text formatting using ActionScript we&#8217;ll use the ActionScript method but if you feel more comfortable creating the text field initially in the GUI then that&#8217;s perfectly fine, it&#8217;ll still work for this example.<\/p>\n<p>So, as a minimum we&#8217;re going to need an import: (depending on your setting this may be done automatically as required, but if not, make sure you insert the import statement yourself)<\/p>\n<p>[codebox 1]<\/p>\n<p>Now, we are able to create the textfield (a TLF Textfield in this case) below our import statement:<\/p>\n<p>[codebox 2]<\/p>\n<p>So, just to briefly explain the code above. \u00a0First we create a variable of type TLFTextField and call that variable txtOurText. \u00a0We then assign this a value of new TLFTextField() &#8211; this basically creates a new text field instance to assign to the variable using the TLFTextField constructor. \u00a0Then, we set the initial contents of our text field, in this case &#8211; DPS Computing Limited Rocks! . \u00a0We set its initial position on stage to be at 30, 30. \u00a0Please note, the initial position of the textfield has been set, however the text box doesn&#8217;t exist on the stage yet so at the moment, that&#8217;s just a theoretical position which indicates where the textfield will go when it is added to the stage. \u00a0This is where the addChild() function comes in which finally adds the textfield to the stage using the criteria that we specified directly above it.<\/p>\n<p>Right, so at the moment our text is probably going to be quite boring. \u00a0Probably standard paragraph sized black text. \u00a0You preview should look just as exciting as below (please note, if it looks more exciting you&#8217;ve probably done it wrong ;)).<\/p>\n<p><a href=\"http:\/\/dpscomputing.com\/wordpress\/wp-content\/uploads\/2012\/07\/cs5textformatas3-2.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1139\" data-permalink=\"https:\/\/www.dpscomputing.com\/blog\/2012\/07\/05\/how-to-format-text-in-flash-using-actionscript-3\/cs5textformatas3-2\/\" data-orig-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformatas3-2.png\" data-orig-size=\"551,428\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"cs5textformatas3-2\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformatas3-2-300x233.png\" data-large-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformatas3-2.png\" tabindex=\"0\" role=\"button\" class=\"aligncenter size-full wp-image-1139\" title=\"cs5textformatas3-2\" src=\"http:\/\/dpscomputing.com\/wordpress\/wp-content\/uploads\/2012\/07\/cs5textformatas3-2.png\" alt=\"\" width=\"551\" height=\"428\" srcset=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformatas3-2.png 551w, https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformatas3-2-150x116.png 150w, https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformatas3-2-300x233.png 300w\" sizes=\"auto, (max-width: 551px) 100vw, 551px\" \/><\/a><\/p>\n<p>Now for the exciting stuff (well as exciting as it&#8217;s going to get formatting text anyway!). \u00a0The common mistake and cause of frustration at this point is to start trying to apply new values to attributes of txtOurText. \u00a0Hold on one second!!! \u00a0That won&#8217;t work. \u00a0And you&#8217;ve possibly already realised that after you became that frustrated that it wasn&#8217;t working that you used Google to perform a search and found this guide!!<\/p>\n<p>Anyway, I digress, back on topic. \u00a0To avoid this common mistake, we don&#8217;t apply changes to the style of the textfield directly to the textfield. \u00a0This may sound a little bit odd, but bear with me.<\/p>\n<p>We want to do a bit of text formatting, so logically we look to the TextFormat class &#8211; and low and behold, it actually exists, hurrah! \u00a0Now, lets concentrate on doing some formatting and worry about the applying further on.<\/p>\n<p>So, firstly, we need another import. \u00a0Again depending on your settings, this may be inserted automatically, but just to be on the safe side you can put it in anyway. \u00a0If you&#8217;re copying and pasting this code in, the imports are not likely to be automatically included, irregardless of your settings. \u00a0This is due to Flash only checking whether it needs any more inputs as your type in new code &#8211; and copying and pasting doesn&#8217;t count as typing unfortunately. \u00a0From past experience, the automatic import statements fail to materialise occasionally, typically resulting in 3000 runtime errors at which point you are ready to throw your computer out of the second floor window (and sometimes, when it gets really bad, you also consider following the computer!). \u00a0So, import statement:<\/p>\n<p>[codebox 3]<\/p>\n<p>Tada! \u00a0Text formatting ability activity. \u00a0Now, we need a variable to for the text formatting (of type Text Format) and we need to set some of its attributes. \u00a0Please note, in the following code we are setting the attributes of the Text<em><strong>Format<\/strong><\/em> variable <span style=\"text-decoration: underline;\"><strong>not<\/strong><\/span> the Text<em><strong>Field<\/strong><\/em> variable:<\/p>\n<p>[codebox 4]<\/p>\n<p>Right so here we&#8217;ve created the new text format variable (fmtOurText &#8211; thats &#8216;format our text&#8217; when spoken, not something like &#8216;furmert our text&#8217; ;)) and we&#8217;ve decided that we don&#8217;t like (and hence want to change) the colour, typeface and size of our so-far boring text. \u00a0For the non-Americans of us reading, please remember that although we use colo<strong><span style=\"text-decoration: underline;\">u<\/span><\/strong>r, in Flash its the US English spelling, in line with most other programming languages, so use\u00a0<strong>color<\/strong> instead (please note, automatic spell checkers that sit &#8216;on top&#8217; of all your programs may try to correct your spelling if it&#8217;s set to British English, disable them if possible or they can become frustrating! \u00a0Mine just automatically corrected my spelling of colour (when I wanted the US English spelling), obviously in an attempt to make me look silly!). \u00a00x0000FF is the hex for blue so we&#8217;re going to have bigger, bluer and more aesthetically pleasing (Verdana) text.<\/p>\n<p>Now, we have our textfield, our text and a nice new format. \u00a0Now we just need the format to apply to our text field. \u00a0This can be done by using the following simple statement:<\/p>\n<p>[codebox 5]<\/p>\n<p>Change the width that we previously set to 500 (we need this so when the text grows we&#8217;ll be able to see it all still). \u00a0After you&#8217;ve done that, give it a test run and you should see something like this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1140\" data-permalink=\"https:\/\/www.dpscomputing.com\/blog\/2012\/07\/05\/how-to-format-text-in-flash-using-actionscript-3\/cs5textformat-3\/\" data-orig-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformat-3.png\" data-orig-size=\"553,426\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"cs5textformat-3\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformat-3-300x231.png\" data-large-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformat-3.png\" tabindex=\"0\" role=\"button\" class=\"aligncenter size-full wp-image-1140\" title=\"cs5textformat-3\" src=\"http:\/\/dpscomputing.com\/wordpress\/wp-content\/uploads\/2012\/07\/cs5textformat-3.png\" alt=\"\" width=\"553\" height=\"426\" srcset=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformat-3.png 553w, https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformat-3-150x115.png 150w, https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformat-3-300x231.png 300w\" sizes=\"auto, (max-width: 553px) 100vw, 553px\" \/><\/p>\n<p>Beautiful isn&#8217;t it! \u00a0Well at least better than what we had. \u00a0And we&#8217;ve only just scratched the surface here. \u00a0You can use the TextFormat class to change all the attributes you can think of (and more) that relate to text fields and text holding containers in general. \u00a0Obviously the main benefit of formatting text in this way is the ability to change text dynamically at runtime. \u00a0This can be for the purposes of animating text, grabbing the users attention, highlighting an error or responding to an event or condition.<\/p>\n<p>There&#8217;s a few important notes to add about ordering of the code. \u00a0All imports go at the top, nothing before them. \u00a0Then the TextFormat code, followed by the TextField code. \u00a0And make sure that you apply the format before you add the text field to the stage.<\/p>\n<p>Now if you&#8217;ve been confronted be the dreaded errors, sample shown below&#8230;.<\/p>\n<p><a href=\"http:\/\/dpscomputing.com\/wordpress\/wp-content\/uploads\/2012\/07\/cs5textformatas3-5.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1142\" data-permalink=\"https:\/\/www.dpscomputing.com\/blog\/2012\/07\/05\/how-to-format-text-in-flash-using-actionscript-3\/cs5textformatas3-5\/\" data-orig-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformatas3-5.png\" data-orig-size=\"1081,223\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"cs5textformatas3-5\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformatas3-5-300x61.png\" data-large-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformatas3-5-1024x211.png\" tabindex=\"0\" role=\"button\" class=\"aligncenter size-large wp-image-1142\" title=\"cs5textformatas3-5\" src=\"http:\/\/dpscomputing.com\/wordpress\/wp-content\/uploads\/2012\/07\/cs5textformatas3-5-1024x211.png\" alt=\"\" width=\"550\" height=\"113\" srcset=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformatas3-5-1024x211.png 1024w, https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformatas3-5-150x30.png 150w, https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformatas3-5-300x61.png 300w, https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformatas3-5.png 1081w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<p>&#8230;don&#8217;t despair! \u00a0We can fix it.<\/p>\n<p>Look at the working code listing below if you&#8217;re having trouble getting the code in the right order:<\/p>\n<p>[codebox 6]<\/p>\n<p>If you&#8217;ve still got errors, check you&#8217;ve not missed out any semi colons or quotation marks. \u00a0You&#8217;re code should be coloured similar to below if you&#8217;ve punctuated it correctly:<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/dpscomputing.com\/wordpress\/wp-content\/uploads\/2012\/07\/cs5textformat-4.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1141\" data-permalink=\"https:\/\/www.dpscomputing.com\/blog\/2012\/07\/05\/how-to-format-text-in-flash-using-actionscript-3\/cs5textformat-4\/\" data-orig-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformat-4.png\" data-orig-size=\"983,513\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"cs5textformat-4\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformat-4-300x156.png\" data-large-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformat-4.png\" tabindex=\"0\" role=\"button\" class=\"aligncenter  wp-image-1141\" title=\"cs5textformat-4\" src=\"http:\/\/dpscomputing.com\/wordpress\/wp-content\/uploads\/2012\/07\/cs5textformat-4.png\" alt=\"\" width=\"786\" height=\"410\" srcset=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformat-4.png 983w, https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformat-4-150x78.png 150w, https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2012\/07\/cs5textformat-4-300x156.png 300w\" sizes=\"auto, (max-width: 786px) 100vw, 786px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>And if you&#8217;ve truly lost the will to live, you can download the <a href=\"http:\/\/www.dpscomputing.com\/blogextras\/tutorials\/cs5-textformatting-as3\/cs5-textformatting-as3.fla\" target=\"_blank\">fla file<\/a> (and heres the <a href=\"http:\/\/www.dpscomputing.com\/blogextras\/tutorials\/cs5-textformatting-as3\/cs5-textformatting-as3.swf\" target=\"_blank\">swf<\/a> as well) and have a play around with it!<\/p>\n<p>Now hopefully everything hasn&#8217;t come crashing down and you&#8217;ve managed to run the example in the tutorial error free. And hopefully some of your previous stress has been relieved due to finding out the solution to your AS3 formatting problems and this guide interspersed with witty comments ;).<\/p>\n<p>Happy developing! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Although Flash applications and development has had a mixed press over the past couple of years, it still remains a popular and versatile technology, with the CS6 series of applications released by Adobe recently.&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[29],"tags":[570,569,571,572,573,574,578,564,566,563,565,580,561,562,567,576,579,568,575,577],"class_list":["post-1136","post","type-post","status-publish","format-standard","hentry","category-tutorials","tag-action-script","tag-actionscript","tag-actionscript-3","tag-as3","tag-as3-text","tag-as3-text-formatting","tag-computing-tutorial","tag-creative-suite-5","tag-creative-suite-6","tag-cs5","tag-cs6","tag-dynamic-text-formatting","tag-flash","tag-flash-cs5","tag-flash-cs6","tag-flash-text-formatting","tag-runtime-text-formatting","tag-text-formatting","tag-tlftext-formatting","tag-tutorial"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3nsfA-ik","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/posts\/1136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/comments?post=1136"}],"version-history":[{"count":2,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/posts\/1136\/revisions"}],"predecessor-version":[{"id":1143,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/posts\/1136\/revisions\/1143"}],"wp:attachment":[{"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/media?parent=1136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/categories?post=1136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/tags?post=1136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}