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

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)
 

Attachments

  • Store CSBuilder Text v2.zip
    439.2 KB · Views: 544
Last edited:

AnandGupta

Expert
Licensed User
Longtime 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
 

Gavin

Member
Licensed User
Longtime User
Firstly, I know this is an Old Thread, but I am not actually asking a Question.

Thank you Ivan for sharing this brilliantly simple idea. It works a "treat".
I only small problem was that sometimes the formatting failed.
I did notice that it was only when I had the "Pop" character, ">" as the very last character in the sequence.

I the noticed you used:-
B4X:
Do While i < txt.Length - 1 'until cursor reaches the end of the text

In this case, the "-1" is not necessary and was causing the loop to finish one character too soon thus missing the final ">"
I commented out the -1 with immediate success.

Once again, thank's

Gavin
 
Top