14 Replies Latest reply on May 1, 2008 2:09 PM by rafa@mediatech

    Resizing fonts to fit text boxes

    rafa@mediatech Level 1
      Hello everyone:
      I have a project in which the information presented on the .exe refers to a notepad that has been created by my customer. My issue is:
      I made the text box for the person's name (there are more text boxes, but the name is the only one that is an issue) of the person to hold 13 characters comfortably without breaking into another line below. My question is, how can I make the text box fit more characters in the same space, by resizing the font when there are more characters than what fit perfectly.

      By looking through the forums I found that someone learn how to do it, but they didnt explain how.
      heres the link of that post:
      http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=11&catid=186&threadid =1198926&highlight_key=y&keyword1=size

      Any ideas?

      Thanks!
        • 1. Re: Resizing fonts to fit text boxes
          rafa@mediatech Level 1
          Any ideas...anyone?
          • 2. Re: Resizing fonts to fit text boxes
            Level 7
            > Any ideas...anyone?

            You might have to be a little patient. Not everyone is in your timezone,
            or is waiting to fix your problems.

            Try the following attached to your #text sprite:
            --
            property spriteNum
            property myMember
            property myWidth

            on beginSprite me
            myMember = sprite(spriteNum).member
            myWidth = myMember.width
            end

            on keyUp me
            tPos = myMember.charPosToLoc(myMember.char.count + 1)[1]
            repeat while tPos > myWidth
            myMember.fontSize = myMember.fontSize - 1
            tPos = myMember.charPosToLoc(myMember.char.count + 1)[1]
            end repeat
            end
            • 3. Re: Resizing fonts to fit text boxes
              Level 7
              Can't you just count the number of characters using something like:

              NameLength = member("Names").line .char.count

              and adjust the font size based on that number using something like:

              case NameLength of
              13:
              member("Names").fontSize = 12
              14:
              member("Names").fontSize = 10
              .
              .
              .
              end case
              • 4. Re: Resizing fonts to fit text boxes
                Level 7
                > Can't you just count the number of characters using something like:

                Not unless you're using a fixed-width font.
                • 5. Re: Resizing fonts to fit text boxes
                  rafa@mediatech Level 1
                  Hey everyone!
                  Sean & Word One - Denver:
                  Thanks for your reply, I will try it tomorrow morning when I get back to the office. I tried looking everywhere for how to look for character counts in Director, even on the text formating section the closest thing they talked about is about character spacing.

                  The text box needs to stay the same width and start off at font size 19. Once there are more than 13 characters is when I needed it to start decreasing the size of the font so that it shows the complete name without getting cutoff or breaking into a second line.

                  I will try it and let you know. Thanks!
                  • 6. Re: Resizing fonts to fit text boxes
                    rafa@mediatech Level 1
                    Hey guys:
                    I tried to use both ideas and I get a "Property not fount" prompt window.

                    Sean:
                    The text boxes get fill with the information on the notepad specific to lines on the notepad. For the "name" member, it looks for what is on line 1 of the notepad, for the telephone # looks for line 2, etc... and fills up the text box using my import code "on startMovie()" Would this affect how the "on keyUp me" on your code works?.

                    Heres is part of my import code...is longer than this, but this is the part the fills in the "name" member.

                    on startMovie()
                    ImportFiles ()
                    end

                    on ImportFiles ()
                    --Find Title
                    TitleFileTemp = "@" & fileDelimiter &"Documents" & fileDelimiter & "Title" & TitleRegion & ".txt"
                    if baFileExists(TitleFileTemp) then
                    TitleFile = TitleFileTemp
                    end if

                    if baFileExists(TitleFile) then
                    set myFile = new(xtra "fileio")
                    openFile (myFile, TitleFile, 1)
                    tempvar = readLine(myFile)
                    member("Name").text = tempvar.line[1]
                    NULL = readChar(myFile)
                    end if
                    end

                    I thought I should try to add it to a script sprite on the time line, but that didn't work either.

                    thanks!
                    • 7. Re: Resizing fonts to fit text boxes
                      Level 7
                      What you could do instead is create the text in a text member that
                      1) isn't displayed
                      2) is easily long enough to take all the text on one line
                      3) uses a large font size (3x the final font size seems to work ok)

                      and use imaging Lingo to shrink the appropriate section (see charPosToLoc to
                      find the length) into a bitmap which you display.

                      Presumably there are sensible reasons for not making the text member long
                      enough in the first place?

                      Andrew


                      • 8. Re: Resizing fonts to fit text boxes
                        rafa@mediatech Level 1
                        Hey guys:
                        I got it to work by adding something like this into the actual text member...it is not pretty, but it works.

                        on beginSprite me
                        member("Name").fontsize = 17
                        if member("Name").text.length > 14 then
                        member("Name").fontsize = 16
                        if member("Name").text.length > 15 then
                        member("Name").fontsize = 15
                        end if
                        end if
                        end

                        It sucks that I would have to check for every situation and see which font size works with which amount of characters.
                        • 9. Re: Resizing fonts to fit text boxes
                          Production Monkey Level 3
                          "It sucks that I would have to check for every situation and see which font size works with which amount of characters. "

                          NO! That is called programming. It only seems like magic because it is very fast.
                          • 10. Re: Resizing fonts to fit text boxes
                            stephen_ Level 1
                            You can also adjust the charSpacing instead of the fontSize

                            Something like;
                            tPos = myMember.charPosToLoc( myMember.char.count + 1 )[1]
                            myMember.charSpacing = myMember.charSpacing - ( float( tPos -myMember.width ) / myMember.char.count )

                            • 11. Re: Resizing fonts to fit text boxes
                              rafa@mediatech Level 1
                              Production Monkey:
                              LOL...I know it is call programming, but shouldn't I be able to "program" something that would work in all situations, instead of telling the program every situation my self?...sorry, I am more of a graphic designer than a programmer...but I am getting more interested in programming, people in interviews seem to like you more if you can do both.

                              stephen_:
                              Wouldnt that just affect the spacing...so then if there are too many characters, they will only be on top of each other?....I tried the code but it give me a "variable used before assigned value..." after " tPos=mymember.?"


                              Thanks!
                              • 12. Re: Resizing fonts to fit text boxes
                                Production Monkey Level 3
                                rafa,

                                Sorry. There has been a bit too much whining on the forums lately and I took my annoyance out on you.

                                "...shouldn't I be able to "program" something that would work in all situations, instead of telling the program every situation my self?..."

                                Yes.

                                That is where global functions are useful. You solve the problem in a general way and then you can reuse that code in all future programs.

                                I have a movie script called "Text Functions" that I have developed over the years and I add to all my projects. Things like: Trimming white space, replacing strings and characters, formatting numbers, etc.

                                To write the function, you need to define the problem. You need to know what is the largest font size that you can apply to a string that will not exceed a certain width in pixels. You need a function that will return that information.

                                Our parameters are then: "the string","the width of the text member", "the largest font size to try", and "what font ".

                                The only way to determine this is to start with the largest font size and work your way down to smaller font sizes until you find the one that fits without going over the width of the text member. Something like:

                                -- Returns the largest font size that can be applied to the string passed in without exceeding the MaxWidth
                                -- aString:#String The string of text you want to run the function on.
                                -- FontName:#String ie "arial", "Times New Roman", etc.
                                -- MaxWidth: #Integer The width in pixels that you do not want the text string to exceed.
                                -- MaxFontSize:#Integer The largest font size to apply to the text.
                                on getMaxFontSize aString, FontName, MaxWidth, MaxFontSize
                                TempTextMem = new(#text)
                                TempTextMem.Font = FontName
                                TempTextMem.Text = aString

                                repeat with CurFontSize = MaxFontSize down to 6
                                TempTextMem.Fontsize = CurFontSize
                                if TempTextMem.charPosToLoc(TempTextMem.char.count + 1)[1] <= MaxWidth then exit repeat
                                end repeat

                                TempTextMem.erase()
                                return CurFontSize
                                end getMaxFontSize

                                To use the function you would write something like:

                                FontToUse = getMaxFontSize("Hello World", "Arial", 76, 18)
                                • 14. Re: Resizing fonts to fit text boxes
                                  rafa@mediatech Level 1
                                  Production Monkey:
                                  Wow!. thanks for the explanation. The text boxes have specific fonts that I need to use. I already attached these fonts to my director project. So, on the function equation, I just replace the "Arial" name with "My font name", right?

                                  Thanks!