11 Replies Latest reply on May 20, 2009 7:09 AM by Chunick

    Bold text formatting & saving as plain text

    tem009

      I have a text box where user can write text in edit mode, then text is saved to database as plain text, then later it is displayed in read mode to other users. Now I would like to add an option for user to put bold text formatting to some parts of the text that is written. Is this possible when text is saved as a plain text?

       

      If that's not possible, I think even using html-tags would be better than not giving any options, so what tags should be used for bold text and how I can display tags correctly in Director text field? I have tried member("textfield").html, but that is displaying all tags as normal text.

       

      Also, how I can add a button which adds a bullet list "ball" to the text field where focus is?

        • 1. Re: Bold text formatting & saving as plain text
          Chunick Level 3

          You have to create a routine that will allow you to select and set bold on the text as people are typing... not unlike the interface used here to format the posts.

           

          Use:

          member(x).selectedText.fontStyle = [#bold]

           

          * where x is the member's name or number.

           

          the selectedText property of the text member creates a direct reference to that chunk of text in the member... we can see that if we did this in the message window after selecting some text (assuming my text member is named "text"):

           

          put member("text").selectedText

          -- <Prop Ref 1 58816fc>


          By the Use example above you can probably extrapolate that other text/font formatting properties that operate at the character level can be directly accessed this way and set to the current text selection, ie: .font, .fontSize, etc.

           

          When you're done and going to save, just save the .html of the member to the db:

           

          someVar = member(x).html

          -- code here to save the string variable to the db - make sure to escape characters such as single/double quotes... whatever the escape characters are for the particular db engine.

          • 2. Re: Bold text formatting & saving as plain text
            tem009 Level 1

            Thanks for your reply. So if the text is saved as html, also the information about bold text formatting is saved?

             

            Earlier I found this link http://director-online.com/buildArticle.php?id=463 which tells about formatting text with Lingo. If I create a text editor with that guide, and text is saved to the database as html, should it display correctly later in the read mode?

            • 3. Re: Bold text formatting & saving as plain text
              Chunick Level 3

              I'm quite familiar with that article. It's great except for one small point: the selection code has some issues when you really test it out... use my code instead to apply the formatting, for example:

               

              DOUG Article
              My Code
               pMem.char[pStartChar..pEndChar].fontStyle = styleList
               pMem.selectedText.fontStyle = styleList
               pMem.char[pStartChar..pEndChar].fontSize = value
               pMem.selectedText.fontSize = value
               pMem.char[pStartChar..pEndChar].color = value
               pMem.selectedText.color = value

               

              ... and so on...

              • 4. Re: Bold text formatting & saving as plain text
                tem009 Level 1

                Okay thanks. I created a much simpler code to my project, because I didn't need all the features that example offered. It works fine, except one little problem. I have two embedded fonts, one font which is used for a plain text and the other one is used for a bold text. The "Bold style on/off" button switches between these two fonts. It works fine, but when the user comes to edit an earlier text which was saved to database, there seems to be some empty characters in the text field after the written text and when the user clicks somewhere in the text field outside the written text and begins to write, the font is switched to Times New Roman.

                 

                Is there a way that the system could check the font of the written character and if it isn't embedded plain or bold font, it would switch the font to the embedded plain font?

                • 5. Re: Bold text formatting & saving as plain text
                  Sean_Wilson Adobe Community Professional

                  When your text is loaded from the DB you could check the font of the last character, or remove as many whitespace chars from the end as you need to.

                  • 6. Re: Bold text formatting & saving as plain text
                    Sean_Wilson Adobe Community Professional

                    There is a bug when grabbing the rtf of a text member - which is how I suspect you're saving existing text to your DB - where Director adds an extra RETURN after your text. You can either remove the last instance of "\par" from the rtf before saving it to the DB, or trim the trailing RETURN character when fetching it back from the DB.

                    • 7. Re: Bold text formatting & saving as plain text
                      tem009 Level 1

                      The text is saved as html. When I look the text in database editor, I can see one empty space after the  last </font> tag and two empty spaces between </body> and </html> tags and two empty spaces after </html> tag in the end of the raw text. Those areas don't have any font tags, and they're probably causing the font to change to the default Times New Roman (when the text is loaded to the text field from database, there is 5 empty spaces after the actual text with default font).

                       

                      Is there a way to solve this, maybe by forcing the font to be always one of the embedded fonts or somehow removing all those spaces after the written text?

                      • 8. Re: Bold text formatting & saving as plain text
                        Sean_Wilson Adobe Community Professional
                        The text is saved as html.

                        <snip>

                         

                        Is there a way to solve this ... ?

                        I think you should try saving and loading member.rtf instead of member.html - remembering to strip the introduced trailing RETURN character before or after.

                        • 9. Re: Bold text formatting & saving as plain text
                          fazstp Level 2

                          on formatBugChars text_member, valid_fonts

                            case ilk( text_member ) of

                           

                              #integer, #string:

                                text_member = member( text_member )

                           

                            end case

                           

                            if NOT ( ilk( text_member ) = #member ) then

                              return FALSE

                            end if

                           

                            if NOT ( text_member.type = #text ) then

                              return FALSE

                            end if

                           

                            if NOT ( ilk( valid_fonts ) = #list ) then

                              return FALSE

                            end if

                           

                            char_count = text_member.char.count

                           

                            repeat with char_num = char_count down to 1

                              last_char = text_member.char[char_num].ref

                             

                              if valid_fonts.getOne( last_char.font ) then

                                text_member.char[char_num + 1..char_count].font = last_char.font

                               

                                return TRUE

                              end if

                            end repeat

                           

                            return TRUE

                           

                          end formatBugChars

                          • 10. Re: Bold text formatting & saving as plain text
                            tem009 Level 1

                            Thanks all, I saved the text as rtf and that seems to work. How I can add a bullet list "ball" to a text field between characters, to a place where focus currently is?

                            • 11. Re: Bold text formatting & saving as plain text
                              Chunick Level 3

                              pMem.selectedText.setContents(numToChar(149))

                               

                              -- 149 works on PC... not sure if it's the same on mac as anything higher than 128 is the extended ASCII table and will be different between fonts/OSes..

                               

                              just checked it out here: http://academic.evergreen.edu/projects/biophysics/technotes/program/ascii_ext-mac.htm

                              and it looks like it's 165.