{"id":508,"date":"2010-04-23T09:52:49","date_gmt":"2010-04-23T16:52:49","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=2904"},"modified":"2010-04-23T09:52:49","modified_gmt":"2010-04-23T16:52:49","slug":"textlinemetrics","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=508","title":{"rendered":"TextLineMetrics"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar word:String = &quot;TextLineMetrics are useful&quot;;\r\nvar letters:Array = word.split(&quot;&quot;);\r\n\r\nvar pre:TextField;\r\nfor (var i:int = 0; i&lt;letters.length; i++){\r\n\tvar t:TextField = new TextField();\r\n\tt.defaultTextFormat = new TextFormat(&quot;Arial&quot;, 40);\r\n\tt.autoSize = TextFieldAutoSize.LEFT;\r\n\tt.textColor = int(Math.random() * 0xFFFFFF);\r\n\tt.text = letters&#x5B;i];\r\n\tif (pre){\r\n\t\tvar metrics:TextLineMetrics = pre.getLineMetrics(0);\r\n\t\tt.x = metrics.width + pre.x;\r\n\t}\r\n\tpre = t;\r\n\taddChild(t);\r\n}\r\n<\/pre>\n<p>Sometimes you need to do something to a TextField one letter at a time. One way to do this is to create a separate TextField for each letter and position them based on the TextLineMetrics object. This snippet creates textFields for a string and colors each TextField randomly.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/metrics.jpg\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>var word:String = &quot;TextLineMetrics are useful&quot;; var letters:Array = word.split(&quot;&quot;); var pre:TextField; for (var i:int = 0; i&lt;letters.length; i++){ var t:TextField = new TextField(); t.defaultTextFormat = new TextFormat(&quot;Arial&quot;, 40); t.autoSize = TextFieldAutoSize.LEFT; t.textColor = int(Math.random() * 0xFFFFFF); t.text = letters&#x5B;i]; if (pre){ var metrics:TextLineMetrics = pre.getLineMetrics(0); t.x = metrics.width + pre.x; } pre = t; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,21],"tags":[3,14,4],"class_list":["post-508","post","type-post","status-publish","format-standard","hentry","category-string-manipulation","category-strings","tag-actionscript","tag-as3","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=508"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/508\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}