4 Replies Latest reply on Aug 4, 2008 1:17 PM by cnunciato

    Get the Coordinates of a Character in a TextArea

    cnunciato
      Is there a way to determine what the coordinates of a given character are in a TextArea control? One project I'm working on presents a chunk of text cut off at some number of characters, followed by a LinkButton control (e.g., to view "more"), and I'm having trouble figuring where to place the LinkButton control without knowing the coordinates of the last visible character of the TextArea. Anyone dealt with this one before?

      Thanks in advance...

      Chris
        • 1. Re: Get the Coordinates of a Character in a TextArea
          Gregory Lafrance Level 6
          I don't think you can get accurate position info for individual characters.

          It would be better to have the text control populated from a collection, perhaps with two fields like "summary" and "detail". Then update the text control with the "detail" field contents. If you no longer want the LinkButton to appear, you can set its "visible" and "includeInLayout" properties to false. If you still want it to display, perhaps you are thinking of changing the LinkButton text to something like "Display Summary", you can do that, and if you are using Flex's auto layout capabilities, the LinkButton will be moved when the detailed text displays.

          From an internationalization perspective, absolute positioning of most things having localizable data is a bad idea as well.
          • 2. Re: Get the Coordinates of a Character in a TextArea
            cnunciato Level 1
            When you say "using Flex's auto-layout capabilities," how do you see them applying in this case? What I'm trying to achieve is something like the following:

            Here is the first sentence. Here is the second -- it could
            be of any length, so we need to be able to cut it [LinkButton]

            ... where LinkButton is a control that takes some action. I've tried adding text controls for each "word" and then laying them out in a Repeater-like way, but with scaling, I'm getting strange behavior.

            The requirement is what it is -- words followed with an actionable inline control. Does anyone have any ideas that'd work?

            Thanks,
            Chris
            • 3. Re: Get the Coordinates of a Character in a TextArea
              Gregory Lafrance Level 6
              You don't need to cut the text, just have two versions of the text (summary and detail). If displayed in a TextArea it can wrap if necessary. To the right of the TextArea will be the LinkButton. Have the TextArea and the LinkButton in an HBox and the auto layout capabilities of the HBox should do the trick. Here is an example, though this code would need tweaking for your overall layout and circumstances:
              • 4. Re: Get the Coordinates of a Character in a TextArea
                cnunciato Level 1
                Hm, thanks, but that just places the LinkButton next to the TextArea control, beside and outside of it, not "inside" (or really over) it, as my example should've implied.

                I think I've found a workable solution -- see below, where myCanvas is some Canvas control already defined in MXML somewhere, and myText is a Text control already defined (and populated) as well:

                var numberOfLines:int = myText.mx_internal::getTextField().numLines;
                var lineMetrics:TextLineMetrics = myText.getLineMetrics(numberOfLines - 1);

                var targetX:int = lineMetrics.width + 10;
                var targetY:int = (numLines - 1) * lineMetrics.height;
                var myLinkButton:LinkButton = new LinkButton();
                myLinkButton.label = "More...";
                myCanvas.addChild(myLinkButton);

                Still testing, but TextLineMetrics seems to have done the trick.

                If anyone has a better approach, let me know! Thanks much.