12 Replies Latest reply on Aug 24, 2007 9:13 PM by Greg Dove

    Displaying formated code in flash?

    DiscipleEXX
      I'm trying to display code chunks in Flash, not sure how to do this, or how to find the solution though.

      Basically I just want to display dynamic selectable code similar to how its displayed like this:



        • 1. Re: Displaying formated code in flash?
          DiscipleEXX Level 1
          The code above is meaningless, i just want to know how to format my text like the above in flash.

          Any help is much appreciated!
          • 2. Re: Displaying formated code in flash?
            funonmars Level 1
            You can use style sheets in Flash now, as in
            var codeStyle:StyleSheet = new StyleSheet();
            codeStyle.parseCSS(myURLLoader.data)
            myTextField.txt.styleSheet= codeStyle;

            Then create your external style sheet. Not all tags are supported, but enough are to make it look like code.
            • 3. Re: Displaying formated code in flash?
              DiscipleEXX Level 1
              hmm it sounds complex... what if I just want to display the text, without formatting. Like:

              var text1 = "<form name="search_form" action="/search" method="post" onsubmit="return checkSearchForm(this);">
              <input type="text" name="q" value="" id="term" />
              <input type="hidden" id="searchall" name="searchall" value="0" />
              <input type="hidden" id="searchbyrelevance" name="searchbyrelevance" value="0" />
              <input type="submit" class="bttn" id="srchBttn" alt="" value="Search" />
              </form>"

              but the above code is broken, because it uses characters like "" which break the string format inside of flash. I just want all those characters displayed when I set the dynamic text to it, but am not sure how to achieve it.
              • 4. Re: Displaying formated code in flash?
                Greg Dove Level 4
                If you're doing that a lot the best way is probably to load the data in from an external text or xml file.

                You can change string delimiters, but I don't know a way to have a string split across lines in flash

                Another way is to escape the internal quotes

                e.g. trace("\"") will output:"

                But whereever I can I prefer to use a different delimiter... ie. single quotes if the string has double quotes inside (and no single quotes inside) or vice versa. If you have both single and double quote chars as part of the string then you have no choice (actually you do - String.fromCharCode, but its not so easy), you need to use an escape backslash. \" or \'

                • 5. Re: Displaying formated code in flash?
                  DiscipleEXX Level 1
                  Thanks for the help, I feel a little more confident, but im still not sure. I'm trying to set a dynamic text object with a block of code, so that the user can copy it and use it himself. I tried using your code example, but the text comes up as blank, im not sure why.

                  Basically i just have a dynamic text block with the variable as 'txt'

                  I just want to set that dynamic text to something like:

                  txt = text1

                  (where text1 = what you had above)

                  I don't really need line breaks, a solid block of text would be ok so long as it wrapped. If I need to use info from an external text file, what would be the easiest and simplest way to do that?
                  • 6. Re: Displaying formated code in flash?
                    Greg Dove Level 4
                    Its up to you about the line breaks of course...but I would proabably incude them.

                    If your code snippet is in a text file called : codeExample.txt

                    Then you could just use this:
                    • 7. Displaying formated code in flash?
                      clbeech Level 3
                      Also, two other things about your textField's assignment:

                      1) use the 'text' property to assign value: txt.text = text1;

                      2) make sure you have embedded a font in the dynamic field, or it won't show up.

                      EDIT: PS. above GWD was using a trace statement to print this string to the output panel, that's different than using a textfield on the stage.
                      • 8. Re: Displaying formated code in flash?
                        DiscipleEXX Level 1
                        Ok I feel like I'm so close heh...

                        So what exactly would be inside the codeExample.txt file?

                        And what is this: src=src.split("\r\n").join("\r") ?
                        Is that referencing what's inside the txt file?

                        Thanks again for all the help.

                        • 9. Re: Displaying formated code in flash?
                          Greg Dove Level 4
                          inside the codeExample.txt put your code.....

                          Whatever you want in the textfield.

                          The line:
                          src=src.split("\r\n").join("\r")
                          Just fixes the windows carriage returns and line feeds line breaks.
                          Otherwise you end up with double line breaks if its a windows system.
                          I assume it works OK on others because they don't have double character linebreaks.


                          • 10. Re: Displaying formated code in flash?
                            Greg Dove Level 4
                            And you might want to set the textfield's font to a fixed character size font, to get the same effect for code display. Something like courier I think.
                            Otherwise I think it will appear in TimesRoman by defaults
                            • 11. Re: Displaying formated code in flash?
                              DiscipleEXX Level 1
                              Ok i figured it out, sorry, im an idiot.

                              Thanks!
                              • 12. Re: Displaying formated code in flash?
                                Greg Dove Level 4
                                re thanks: you're welcome.

                                For the other part... if you're referring to the fact that you're learning actionscript...don't be so hard on yourself.
                                I just realised that I didn't explain that line of code accurately.
                                src=src.split("\r\n").join("\r")

                                I said:
                                Just fixes the windows carriage returns and line feeds line breaks.
                                Otherwise you end up with double line breaks if its a windows system.

                                Actually I should have said:
                                Just fixes the windows carriage returns and line feeds line breaks.
                                Otherwise you can end up with double line breaks if its a text file that originated from a windows system. E.g. windows notepad.

                                I think that's a little clearer. Its really just to account for the differences in how windows does things.