To download the code, you must agree to the following license:

Copyright 2010, GreenSock, Inc.

"NO CHARGE" NON-EXCLUSIVE SOFTWARE LICENSE AGREEMENT
-----------------------------------------------------------------------------
PLAIN ENGLISH SUMMARY:

  1. You may use the code at no charge in commercial or non-commercial web sites, games, components, applications, and other software as long as end users are not charged a fee of any kind to use your product or gain access to any part of it. If your client pays you a one-time fee to create the site/product, that's perfectly fine and qualifies under the "no charge" license. If end users are charged a usage/access/license fee, please sign up for a corporate Club GreenSock membership which comes with a special commercial license granting you permission to do so. See http://www.greensock.com/club/ for details.
  2. Use at your own risk. No warranties are offered.
  3. Please respect the copyright.

-----------------------------------------------------------------------------

LEGALESE:

This is a legal agreement between you (either an individual or a single entity) and GreenSock, Inc. ("GREENSOCK") for the proprietary GreenSock ActionScript code known as TweenLite, TweenMax, TweenNano, TimelineLite, TimelineMax, and other code that is available for download at http://www.greensock.com (this code and documentation, as well as any updates which may at GREENSOCK's sole discretion be provided to you from time to time, are referred to in this Agreement as "PROGRAM") By downloading, copying, or otherwise using the PROGRAM, you agree to the terms and conditions of this Agreement. If you do not agree to the terms and conditions of this Agreement, please do not download or use the PROGRAM.

I. LICENSE
A. Subject to the terms and conditions of this Agreement, GREENSOCK hereby grants you a non-exclusive, worldwide, non-transferable right to use the PROGRAM in web sites, games, components and other software applications for which the end user is NOT charged any fees. If you would like to use the code in a commercially licensed software product for which end users are charged a fee (either for usage or access), simply sign up for a corporate Club GreenSock membership at http://www.greensock.com/club/.

II. LIMITATION OF LICENSE AND RESTRICTIONS
A. You agree that you will not disclose, sell, rent, or license the PROGRAM's source code or any derivative works thereof to any third party without the prior written consent of GREENSOCK. Derivative works are defined as modifications that add substantive functionality to the PROGRAM and do not include bug fixes or other minor modifications required to operate the PROGRAM as originally intended. Distribution of the source code as part of your Work Product is acceptable so long as the recipients agree to the terms of this Agreement. You agree not to modify or delete GREENSOCK'S existing copyright notice located in the source code.

B. You may use, duplicate, and distribute the compiled object code as embedded in a Work Product created by you, either for your own use or for distribution to a third party so long as end users of the Work Product are not charged a fee for usage of or access to any portion of the Work Product. Please see http://www.greensock.com/licensing/ for descriptions of Work Products that qualify for the "No Charge" license.

III. CONSIDERATION
A. The license rights granted to you under this Agreement are at no charge, but only in the following circumstances: If on your own behalf or on behalf of a third party you incorporate the PROGRAM into a web site, game, software application, program or any component thereof (collectively, "Work Product"), which in the case of a web site, must be accessible to internet users without payment of a fee of any kind, and in the case of a software application, game, program or component, neither you nor anyone to whom you distribute the Work Product charges a user a fee of any kind to use such Work Product or application, game, program or component into which such Work Product is embedded. The foregoing shall apply regardless of whether you are paid to create such Work Product.

B. In the event your intended use of the PROGRAM does not meet the criteria for the "no charge" license rights set forth in the immediately preceding paragraph, then you are not licensed to use the PROGRAM under this Agreement and must license the PROGRAM under GREENSOCK'S separate fee-based Software License Agreement which is granted to corporate Club GreenSock members (see http://www.greensock.com/club/ for details).

IV. TITLE AND OWNERSHIP
A. The PROGRAM is licensed, not sold, and is protected by copyright laws and international treaty provisions. You acknowledge that no title to the intellectual property in the PROGRAM is transferred to you. You further acknowledge that title and full ownership rights to the PROGRAM, including all intellectual property rights therein, will remain the exclusive property of GREENSOCK and you will not acquire any rights to the PROGRAM except as expressly set forth in this Agreement. You agree that any copies of the PROGRAM you make will contain the same proprietary notices which appear on and in the PROGRAM. You agree that GREENSOCK may identify you as a licensee unless you make a written request otherwise. GREENSOCK hereby grants to you the right to disclose that your product, game, software application, component, or other Work Product makes use of GREENSOCK code (for example, "Powered by TweenLite").

V. DISCLAIMER OF WARRANTY AND LIMITATION OF LIABILITY
A. THE PROGRAM IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. GREENSOCK DOES NOT WARRANT THAT THE FUNCTIONS CONTAINED IN THE PROGRAM WILL MEET YOUR REQUIREMENTS OR THAT OPERATION WILL BE UNINTERRUPTED OR ERROR FREE. GREENSOCK shall not be liable for special, indirect, incidental, or consequential damages with respect to any claim on account of or arising from this Agreement or use of the PROGRAM, even if GREENSOCK has been or is hereafter advised of the possibility of such damages. Because some states do not allow certain exclusions or limitations on implied warranties or of liability for consequential or incidental damages, the above exclusions may not apply to you. In no event, however, will GREENSOCK be liable to you, under any theory of recovery, in an amount in excess of $250. Notwithstanding anything else in this agreement, you agree to indemnify GREENSOCK, its assignees, and licensees, and hold each of them harmless from and against any and all claims, demands, losses, damages, liabilities, costs, and expenses, including legal fees resulting from your use of the PROGRAM.

B. GREENSOCK may, at its sole discretion, provide support services related to the PROGRAM, but has no obligation to do so.

VI. TERMINATION
If you at any time fail to abide by the terms of this Agreement, GREENSOCK shall have the right to immediately terminate the license granted herein, require the return or destruction of all copies of the PROGRAM from you and certification in writing as to such return or destruction, and pursue any other legal or equitable remedies available.

VII. MISCELLANEOUS
A. This Agreement shall be construed in accordance with the laws of the State of Illinois. In the event of any dispute between you and GREENSOCK with respect to this Agreement, we both agree that if we cannot resolve the dispute in good faith discussion, either of us may submit the dispute for resolution to arbitration with the American Arbitration Association before a single arbitrator using the AAA Rules for Commercial Arbitration. The arbitrator's decision is final and can be enforced in any court with jurisdiction over such matters.

B. This agreement represents the complete and exclusive statement of the agreement between GREENSOCK and you and supersedes all prior agreements, proposals, representations and other communications, verbal or written, between them with respect to use of the program. This agreement may be modified only with the mutual written approval of authorized representatives of the parties.

C. The terms and conditions of this Agreement shall prevail notwithstanding any different, conflicting, or additional terms or conditions which may appear in any purchase order or other document submitted by you. You agree that such additional or inconsistent terms are deemed rejected by GREENSOCK.

D. GREENSOCK and you agree that any xerographically or electronically reproduced copy of this Agreement shall have the same legal force and effect as any copy bearing original signatures of the parties.

I'd like to learn how to get bonus plugins, update notifications, SVN access, and more.
To join Club GreenSock, you must agree to the following license:

TextMetrics – Find substring coordinates, line widths, and much more

  • Version: 1.6, Updated 1/29/2008
  • Compatibility: Flash Player 8 and later (ActionScript 2.0)
  • File size added to compressed SWF: about 1kb

Download Now
 
Donate

Join Club GreenSock to get updates and much more

Description

Have you ever needed to find out where the line breaks occur in a dynamic TextField? How about the precise x/y coordinates of a particular phrase/string along with its width & height so that you could highlight it somehow? Or maybe the width of each line of text? That's all possible with the gs.utils.text.TextMetrics class. And to be consistent with the getTextLineMetrics() function in the TextField AS3 class, it also returns the descent, ascent, and leading values too. You must publish to Flash 8 or later for accurate results. HTML text isn't supported. And for right now, phrases/sentences that span multiple lines aren't supported - the measurements you get back indicate where the first part of the phrase/sentence starts. Justified text is not supported either.

Forums

Usage

TextMetrics.getSubstringMetrics(textField_tf:TextField, substring_str:String):Array
  • Description: Finds all instances of a particular substring in the TextField and returns an object for each one with the following properties: x, y, width, height, lineHeight, leading, ascent, descent
  • Parameters:
    1. textField_tf: The TextField containing the substring
    2. substring_str: The substring for which you're searching
TextMetrics.getLineMetrics(textField_tf:TextField):Array
  • Description: Goes through each line of a TextField, measures its width and height and it's x and y coordinates and returns an array of objects (one for each line). The objects have the following properties: x, y, width, height, leading, ascent, descent, and text
  • Parameters:
    1. textField_tf: The TextField you're measuring
TextMetrics.getTextBounds(textField_tf:TextField):Object
  • Description: Returns an object with the xMin, xMax, yMin, and yMax, x, y, textWidth, and textHeight of the TEXT inside of a TextField (NOT the TextField box itself)
  • Parameters:
    1. textField_tf: The TextField whose text you're measuring

Examples

To put a red box over every instance of the word "ActionScript" in the TextField named my_tf, do:

Actionscript:
  1. import gs.utils.text.TextMetrics;</p>
  2. var instances_array = TextMetrics.getSubstringMetrics(my_tf, "ActionScript");
  3. var instance;
  4. for (var i = 0; i <instances_array.length; i++) {
  5.     instance = instances_array[i];
  6.     box(this, instance.x, instance.y, instance.width, instance.height);
  7. }
  8. function box(parent, x, y, width, height):MovieClip {
  9.     var l = parent.getNextHighestDepth();
  10.     var mc = parent.createEmptyMovieClip("box"+l, l);
  11.     mc.beginFill(0xFF0000);
  12.     mc.moveTo (0, 0);
  13.     mc.lineTo (width, 0);
  14.     mc.lineTo (width, height);
  15.     mc.lineTo (0, height);
  16.     mc.lineTo (0, 0);
  17.     mc.endFill();
  18.     mc._x = x;
  19.     mc._y = y;
  20.     mc._alpha = 50;
  21.     return mc;
  22. }

FAQ

  1. Does this class measure text perfectly in every scenario?
    In my tests thus far, as long as you publish to Flash 8 or later, it has proven to be extremely reliable. That being said, Flash is known for rather buggy and inaccurate text mesurement. You might run into situation where this class doesn't perform perfectly but I'm not aware of any specific problems/issues. Please let me know if you run into any.
  2. Do I need to embed fonts for this to work?
    Nope. It seems to work just fine with or without embedded fonts.

Need help?

Feel free to post your question on the forums. You'll increase your chances of getting a prompt answer if you provide a brief explanation and include a simplified FLA file (and any class files) that clearly demonstrates the problem.

Donate
Author: Jack Doyle
Copyright 2009, GreenSock (This work is subject to the terms here.)

Comments (18) RSS

Posted by Bob Corporaal on August 9, 2007

Thanks! This one save me a TON of coding & debugging tonight!

Posted by Dan Efergan on August 16, 2007

Thanks. I was about to set about working out the same, but you’ve already done it and a whole lot more! I’m looking forward to seeing how you went about it.

Posted by Adam on September 7, 2007

This class is great, thanks a lot!

Posted by karim on September 18, 2007

very nice one indeed.
Thanks for the effort

Posted by Martin on September 23, 2007

Darn, I can’t believe it! You’ve found the answer to this utterly frustrating riddle: “what’s my text’s width and height”. All of these functionalities declined for lack of better measurement capabilities. I can’t wait to use this class!

Thanks man.

Posted by Whatever-ishere on November 21, 2007

thanks for the GREAT post! Very useful…

Posted by ... on December 3, 2007

when you add more lines and scroll the text, it will not work properly

Posted by jack on December 4, 2007

Yes, TextMetrics isn’t meant to track TextFields that are changing/scrolling on a continual basis – it determines the metrics at the point when you call it. So if your TextField changes positions or scrolls or repopulates, you should measure it again.

Posted by gamecreature on December 31, 2007

Thank you very much!!
Works very nice!

Posted by Jake on January 28, 2008

Great class Jack, very useful.

I did have one problem with it when I tried to implement the getLineMetrics method. It worked fine for some textfields, but for some it would give each line’s x value as the same as the top line. It turned out that for some of these textfields ‘newline’ was being input as undefined. I replaced ‘newline’ with “\n” and it worked fine.

And before you ask I have no idea exactly what these different textfields had in common, totally baffling! Just in case anyone else has the same issue.

Jake

Posted by jack on January 30, 2008

Thanks for the suggestion, Jake. I updated the class to include “\n” instead of “newline”.

Posted by Andrew Merskin on April 3, 2008

Jack,

This is JUST what I was looking for… did God give you the title of “Most useful Flash class maker”?

An ActionScript 3 version would be lovely!

~ Andew

Posted by Fats on October 23, 2008

Wow… i love you.
thanks for the class !

Posted by PlugFour on March 14, 2009

You are a beast guy! I wish I knew this existed for the past couple years.

Posted by Agustín on April 15, 2009

DUDE!!! thanks yet again… I just keep bumping in your classes out of need, and they are always the bes and easier solution… you should apply to Adobe and teach them a bit about what a designer/developer needs.

or better yet, get some funding from Adobe and keep your awesome work, it’s because of you that Flash keep getting easier and easier to work with.

I totally agree with Andrew: “Most useful Flash class maker”

Posted by yogibali on September 29, 2009

very.. very.. usefull class script … ;) . thx alot..

Posted by Assaf Sahar on November 5, 2009

Thank you for this wonderful class. It saved me a lot of time and trouble. I also use tweenmax and your XML parser and they all great!!!
Please keep doing a great job.

Posted by Rick Blommers on January 18, 2010

Thanks for this class, it works in almost all situations.

Except that I found a problem with calculating the line breaks with lines that contain a dash: ‘-’. Flash seems to word-break lines on a dash. The calculation doesn’t do this, so my line position calculation gets incorrect if I have a string like with a few dashes.

A fix for this is replacing the line:
words_array = lines_array[i].split(” “);

By first replacing all dashes with spaces and then splitting, it seems to work better:
words_array = lines_array[i].split(“-”).join(” “).split(” “);

Of course it wil not be exactly correct, but it worked for me.