A Flash Developer Resource Site

Results 1 to 18 of 18

Thread: Determiing the length of text in a textfield?

  1. #1
    Member
    Join Date
    Jan 2001
    Posts
    54

    Determining the length of text in a textfield?

    I have a 200 pixel wide text field that will be populated with some dynamic text. I would also like to have this text be underlined so that it resembles a HTML link.

    How can I determine the length of the dynamic text so that I can resize the underline to match?

    Thanks in advance.
    Last edited by Danger Madcow; 01-19-2005 at 08:46 PM.

  2. #2
    Member
    Join Date
    Jan 2001
    Posts
    54
    Is this a dumb question or a brain teaser?

  3. #3
    World Kit Vote Holder Abelius's Avatar
    Join Date
    Feb 2002
    Location
    US
    Posts
    963
    Code:
    trace(myTextField.length())
    Cordially,
    Abelius
    www.worldkit.com

  4. #4
    Banned
    Join Date
    Apr 2001
    Location
    Montréal, Québec.
    Posts
    25,397
    Sure about those ()

    trace(myTextField.length);

  5. #5
    Member
    Join Date
    Jan 2001
    Posts
    54
    Originally posted by oldnewbie
    Sure about those ()

    trace(myTextField.length);
    This worked, without the "()".

    What a wonderfully simple answer to my question. Thanks for the help, guys.

  6. #6
    Junior Member
    Join Date
    Nov 2006
    Posts
    14
    yep that works, but is it possible to get the length in pixels?

  7. #7
    ...— —...— —...— —...
    Join Date
    Sep 2000
    Location
    Ottawa, Canada
    Posts
    135
    It's not pretty but ryanmacflashkit has a solution at http://board.flashkit.com/board/show...11#post3825311

    By calculating the (average font width*#length of the string)= the pixel width.

  8. #8
    ...— —...— —...— —...
    Join Date
    Sep 2000
    Location
    Ottawa, Canada
    Posts
    135
    To get the length in pixels:

    trace(myText.textWidth);

    This sipmple line of code will trace the width of the text field.

  9. #9
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    why do u need to know the length at all? just use html tags e.g

    Code:
    this.createTextField("tMessage", 1, 100, 100, 200, 50);
    tMessage.autoSize = true;
    tMessage.html = true;
    tMessage.htmlText = "<u><a href='http://www.hotmail.com'>This is a link to hotmail</a></u>"
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  10. #10
    ...— —...— —...— —...
    Join Date
    Sep 2000
    Location
    Ottawa, Canada
    Posts
    135
    yes it's true I can easily create a text field to a specific dimension. But that's not what I'm doing.

    I have over 50 buttons on the stage. I don't want to be placing them on the stage one by one. Instead I have a master button_mc that contains text and an underline. (along with a few other possible rollover animations. – bullets, background colour ect.) First I place the buttons on stage using attach movie. Then I turn all of the bullets, backgrounds an undelines to a width or a _visible of 0.

    Then using an Array I push text into each button. Since I don't know the length of some of these buttons. I have created a long text field inside the button. onRollover the underline expands to the width of... the text. Not the text field and not the button_mc. all of which are longer than the text.

    textWidth works like a charm. THANKS

    textHeight has also helped me to create an animated background for text that is two or three lines deep.

    Ps checked out your site. Great job.

  11. #11
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    ..glad u like my site...

    the above textfield in the example does not have a specific dimension, it grows to accommodate whatever text is put in it..

    anyway as well as u got it sorted, all is good !!....
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  12. #12
    ...— —...— —...— —...
    Join Date
    Sep 2000
    Location
    Ottawa, Canada
    Posts
    135
    Getting text to resize isn't really my problem. Although that does help keep it clean.

    I'm animating objects under the text. Think of a hint button. rollOver an object and the 'hint' function is invoked. That function attaches the hint_mc to the stage. It then calls an array and publishes the appropriate array text inside the hint_mc. The background of the array is equal to the width of the text plus a bit of padding. Done.

    Without textWidth I was having to make backgrounds equal to the longest string in the Array. Not so nice.

    Thanks!

  13. #13
    ...— —...— —...— —...
    Join Date
    Sep 2000
    Location
    Ottawa, Canada
    Posts
    135
    Dear Mad Cow, Sorry I got off track in your thread. Since you want to make an underline in your Dynamic text why don't you use CSS. Use this to controll your dynamic text.

    p {
    font-size: 9px;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    text-decoration: none;
    color: #FFFFFF;
    margin-left: 5px;
    margin-right: 5px;
    }

    a {

    color: #EC3A00;
    text-decoration: none;
    }

    a:hover {
    color: #F54500;
    text-decoration: underline;
    }

    Here is a simple tutorial on flash and CSS: http://www.kirupa.com/developer/mx2004/css.htm

  14. #14
    Junior Member
    Join Date
    Jul 2007
    Posts
    16

    Unhappy

    I've used textWidth property and it worked verywell,then while continue working in .fla file it failed to give me the right value!!!!
    it gave me a wrong value than the real value of textWidth?!
    does anyone know why?

  15. #15
    ...— —...— —...— —...
    Join Date
    Sep 2000
    Location
    Ottawa, Canada
    Posts
    135

    Question

    are you in As2? Is the difference consistent?

  16. #16
    Junior Member
    Join Date
    Jul 2007
    Posts
    16

    Unhappy textwidth failed unreasonable

    Yes,I'm in As2?
    And difference is inconsistent at all! sometimes it's read right sometimes it's not .
    could size of window (shown part of stage) affect the size of textwidth?!,if so,How can I fix it?!

  17. #17
    Junior Member
    Join Date
    Jul 2007
    Posts
    16
    I think the problem was with the size of the window of swf.
    my function only works well if stage.scaleMode="showAll"; inorder to read textWidth value in properway,but since my final code will be published at html, it doesn't work at all.
    it always fail to read proper value for textwidth?!!!
    I don't know what to do!
    any suggestions.!?

  18. #18
    Junior Member
    Join Date
    Jul 2007
    Posts
    16

    Angry textWidth bug

    I got it.
    textWidth property only works with textfield with Single line property selected or multiline NoWrap.

    for textfield with multiline property selected it'll read different value for same text if text size is changed ,even if no wrap exist.
    it also fails dramatically for other languages than English.

    attached sample fla file to show that.
    with all textfield takes same text string but reads different value dependent on multiline property and creation on run-time.
    click on buttons to change text and observe new values also change the width of textfield and textWidth will change.

    I think such a note should be added to flash help.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center