Android Code Snippet Store CSBuilder formatted text in plain .txt file

Discussion in 'Code Snippets' started by Ivan Aldaz, Apr 19, 2019.

  1. Ivan Aldaz

    Ivan Aldaz Member Licensed User

    Hi all.

    In my app I want to provide instructions and info about the app (credits, licenses, etc.) in several languages, and would like to show the text with some formatting (colored, bold, underlined, textsize, ...). With a plain text file, formatting is not possible, so I searched for the best way to make it. I thought about creating a PDF for each language, but didn't found easy to open PDF files without using an external app, and the same for .rtf formats. So I had to think...

    With CS Builder I could format text, but using many variables for each language (one per title, subtitle, sentence or paragraph). If the texts are long, it's a chaos. Not valid.

    Then I had the next idea: I had "only" to write a text file marking where and how I wanted to format the text, and after parse this text with a code that recognizes these marks.Against all the predictions I had, it was easier than I thought, and found a way to do it in a very simple way, and here it is.

    The method is fully based on CSBuilder, and basically consists of a sub that returns a CSBuilder formatted text by parsing a .txt file, text that previously has been composed with some instructions. The code parses the text char-by-char, looking for the 'key char' "<", which means that a format span is starting, and then applies format until text formatting ends, marked with the char ">". I'm sure that there must be a more elegant way to do this, but for the moment this method covers my needs, and also I think there's no need for not very large texts. The attached file has about 3200 characters, and the code returns the formatted text almost immediately.

    Don't know if there is a method like this already, but if so, my apologies to the author/s.

    Attached is an Example project, that contains the code and two example files, "test2.txt" and "test4.txt". The next instructions for use are also in the sample code.

    Please, feel free to modify code for method improvement.

    Hope you find it useful!


    === INSTRUCTIONS ===

    - Use UTF-8 text to avoid strange chars, better without BOM Character. It's easy to do with Notepad++ (free in Windows Store)

    - Begin a format span using the key char "<", set an argument, type a exclamation (!) to finish the argument definition and finish formatted text with ">" (= cs.Pop):

    - "B", "U" or "K" for Bold, Underlined or striKethrough text Example: <B!boldText>
    - "S" followed by the size digits to adjust textSize. Example: <S18!text with textSize 18>
    - "A" followed by "N", "O" or "C" to set NORMAL, OPPOSITE or CENTER text alignment. Ex: <AO!text with Opposite alignment>
    - "C", followed by the color index in the array or list used <C3!text colored with color index #3 in the array>
    - "G", same as COLOR, but for BackgroundColor
    - "T", followed by the typeface index in the array or list used <T4!text with typeface index #4 in the typefaces array>
    - "L", to create cLickable text. Just type the clickable text between exclamations. Example: I'm creating a <L!clickable text!> example

    - Use "*" to close all active formats (= cs.Popall)
    In your text, you can use the key characters <, >, ! and *, by typing the character "|" before. Examples: |< |!

    - You can set several formats at a time, and not always is needed to 'close' a format (.Pop) to change
    the argument in this format (same as working with CSBuilder).

    - If you're going to put any clickable text, be sure to have a Sub (eventname)_Click in the calling module that handles the Clickable text events. In this example is named "ClickableText_Click".

    - The clickable texts tags are numbered in order or appearance in the text. First tag is 0, second is 1, and so on.

    - Some examples:

    - This is a <k!not valid> text

    - This is text with a <c1!<u!<l!blue and underlined clickable text!>>>, assuming that index of <c1!<b!blue color>> is #1 in the array

    - This is a <s16!<c1!blue text with textSize 16, that <s20!changes to textSize 20 without closing |*s16|* format, and now> returns to textSize <c2!16>, but now* loses all formats

    - <s14!Now, using <t5!<u!typeface with index #5 and |<making it|> underlined>>

    -----------------------------

    EDIT 21.abr.19:

    1 .- Replaced the end-of-argument key char "/" with "!". Using "/" there was a problem in clickable texts with URLs, like "http://testing.com/formatted_text/", where it was necessary to write a "|" char before each slash: <L/http:|/|/testing.com|/formatted_text|//>". Now it's simpler: <L!http://testing.com/formatted_text/!>". Anyway, the code is now prepared for the developer to choose the four key characters.

    2.- Now it's possible to have the list of the clickable texts, so that you can work on them to recognize the type of clickable text (email, URL, etc.) and send an email, open a web browser,...

    New example uploaded (Store CSBuilder Text v2)
     

    Attached Files:

    Last edited: May 19, 2019
  2. AnandGupta

    AnandGupta Active Member Licensed User

    Good idea of using own formatting to get required output. We can use this logic to get HTML, RTF etc. output.

    Thanks for sharing.

    Regards,

    Anand
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice