B4A Library RichString library.

This library contains RichString and RichStringBuilder objects. These objects are analogous to the normal String and StringBuilder. However RichStrings can contain formatting information that controls how they are drawn when used as the text in a View.

This library also contains a RichStringFormatter object. This object, once suitably initialised, will efficiently format RichStrings passed to it. For long strings that require many changes of format this is much more efficient than using the individual RichString formatting commands.


EDIT :- Version 1.1 posted. See post #8 for details.

EDIT :- Version 1.2 posted including RichStringFormatter. See post #13 for details.

EDIT :- Version 1.3 posted. See post #15 for details.

EDIT :- Version 1.4 posted. See post #56 for details.

EDIT (by Erel) :- Version 1.5: https://www.b4x.com/android/forum/threads/richstring-library.10680/page-6#post-475477
 

Attachments

  • RichString.zip
    11.6 KB · Views: 2,330
Last edited by a moderator:

mjtaryan

Active Member
Licensed User
Longtime User
This library contains RichString and RichStringBuilder objects. These objects are analogous to the normal String and StringBuilder. However RichStrings can contain formatting information that controls how they are drawn when used as the text in a View.

This library also contains a RichStringFormatter object. This object, once suitably initialised, will efficiently format RichStrings passed to it. For long strings that require many changes of format this is much more efficient than using the individual RichString formatting commands.


EDIT :- Version 1.1 posted. See post #8 for details.

EDIT :- Version 1.2 posted including RichStringFormatter. See post #13 for details.

EDIT :- Version 1.3 posted. See post #15 for details.

Andrew,

I just ran the demo and read through the code for the demo as well as the online documentation. Looks great, but I do have a couple of questions.

First, in the documentation some of the methods have parameters you call "metachars" defined as String. What do you mean by "metachars"? Would you give an example, please.

Second, I have some lengthy text in files that I will be using and want formatted for display, but don't want -- at least at this point - to use WebView. The material is envisioned as book-like pages that are to be read only. In using RichStringBuilder, what would you recommend as the View of choice for display and how should I structure the text for both input and output? Is there any way to read in a file and output it appropriately?

Finally, I notice under STYLE you have BOLD, ITALIC and BOLD_ITALIC. I see, too, that you have underlining. However, I see no combinations such as BOLD_UNDERLINE. I suspect I can get the same effect by using BOLD and UNDERLINE individually. But, if this is the case, can't I get the same effect as BOLD_ITALIC with BOLD and ITALIC individually. Or were you merely duplicating the android conventions for formatting? Oh, yes, one last question -- what if I want to use a more specific typeface other than monospace, serif or sans serif (i.e. Calibre), do I specify the typeface in the same manner as with the Typeface method in Label or EditText?

Sorry for the sort of stream of consciousness approach. And Thanks.
 

mjtaryan

Active Member
Licensed User
Longtime User
Was looking at the documentation agaon and thought of some methods that it would be nice to have in a future edition of the library. these are:
1. GRAVITY -- includes all of those in other text views along with "Justify." This would allow the gravity to be applied to individual lines of text rather than the whole view.
2. PAGE_END -- that includes pagination and recording of page numbers.
3. INDENT -- including First Line and Hanging.
4. GRAPHIC -- allows insertion of an android.graphic image.
6. And last, the ability to route displayed text around an embedded GRAPHIC.
 
Last edited:

ivanguerr

Banned
B4X:
Sub load_phrase(strLINEA As String)
Dim richlinea As RichString
richlinea.initialize(lbl1.Text & CRLF & "->" & "{C}" & strLINEA & "{C}")
richlinea.color2(Colors.Yellow,"{C}")
lbl1.text=richlinea  'Where lbl1 is a label view
End Sub


I have a sub to add formatting string into a label. The code works well the first call but when I make a second call to the sub, the previous passed string is not formatted, example:

load_phrase("test1") 'test1 is formatting in yellow colour
load_phrase("test2) 'test1 is in white colour and test2 is in yellow colour

is there any way to keep the previous text formatted?
 

mjtaryan

Active Member
Licensed User
Longtime User
I'm wondering if it is possible with the RichString library to do the following formatting of longer text items (pages):

1. Tabs (left, center and right).
2. Justify (left, center, right and full).
3. Indents (such as FirstLine and Hanging).
4. Ordered and unordered lists (i.e. Numbering and Bullets) with hanging indents.
5. Margins (Left, Top, Right, Bottom).

If the library doesn't include these are they even possible or does Android or its object defnitions not have these capabilities?

I'm wondering because I have been asked to produce an app that displays long documents in formatted (RichString) text and would need to (as much as possible) handle these various elements as they occur in the documents.

BTW -- might someone know where I can find info on MS Word formatting (coding) and file structure?

Thanks a bunch.
 

agraham

Expert
Licensed User
Longtime User
RichString deals only with the representation of the text, it carries no positional information. I'm not very good on the Android formatted text capabilities but you could not use this library to build a page formatter or word processor type of application.
 

airblaster

Active Member
Licensed User
Longtime User
Very useful library!
Is there some way to combine it with setellipsize?
So that e.g. the first two lines are ellipsized?
 

Geezer

Active Member
Licensed User
Longtime User
Very useful library, but I'm having a couple of problems.

I wrote a quick routine to try it out but get inconsistent results.

I use this code to generate the text
B4X:
   rs.Initialize(BuildLine("A test of 'Randomness'"))
and this code to make the string
B4X:
Sub BuildLine( str As String ) As String 
   Dim S As String = ""
   For X = 0 To str.Length -1
      S = S & RndCol(str.SubString2(X, X+1))
   Next
   Return S
End Sub

Sub RndCol( ch As Char ) As String
   If ch = " " Then Return ch
   Dim Num As Int = Rnd(0,6)
   If Num = LastNum Then Return ch
   LastNum = Num
   Select Case Num
      Case 0
         Return "{Blue}" & ch & "{Blue}"
      Case 1
         Return "{R}" & ch & "{R}"
      Case 2
         Return "{T}" & ch & "{T}"
      Case 3
         Return "{Red}" & ch & "{Red}"
      Case 4
         Return "{U}" & ch & "{U}"
      Case Else
         Return "{BI}" & ch & "{BI}"
   End Select
End Sub

The text doesn't always appear correctly and I'm not sure why.
 

Attachments

  • 1.png
    1.png
    34.3 KB · Views: 432
  • 2.png
    2.png
    34.1 KB · Views: 411
  • 3.png
    3.png
    34.8 KB · Views: 392
  • 4.png
    4.png
    36.9 KB · Views: 389

Geezer

Active Member
Licensed User
Longtime User
I still don't see where I'm going wrong. Attached is file to take a look. Thanks
 

Attachments

  • rich.zip
    6.8 KB · Views: 271

agraham

Expert
Licensed User
Longtime User
Ah, the penny finally dropped when i looked at your code. Use RichStringFormatter instead of the individual RichString methods. It is much more efficient and easier to use and works well.
B4X:
rs.Initialize(BuildLine("A test of 'Randomness'"))
Label4.Text = rsf.Format(rs)
 

agraham

Expert
Licensed User
Longtime User
Version 1.4 now posted fixes the bug in the individual RichString formatting methods. However RichStringFormatter is recommended as a better way of formatting RichStrings.

In a change I made many months ago but didn't post SpanBehaviours have been added to RichStringFormatter.Format2 and RichString itself. See the RichString.SPAN_XXXX constants
 

mjtaryan

Active Member
Licensed User
Longtime User
RichString deals only with the representation of the text, it carries no positional information. I'm not very good on the Android formatted text capabilities but you could not use this library to build a page formatter or word processor type of application.

Thanks. So, any suggestions on how I can fulfill the objectives of my previous post as well as the RichString abilities? (One of the reasons I asked about MS Word formatting).
 

mjtaryan

Active Member
Licensed User
Longtime User
It's not really my scene but HTML in a WebView?

Thanks for the suggestion. I may need to do that, I guess. I haven't used a webview yet and don't know its capabilities or pecularities. If anyone has considerable experience with webviews, I have some questions that I may like to ask (if I don't get theem answered with a tutorial and viewing the documentation). Thanks.
 

noclass1980

Active Member
Licensed User
Longtime User
Hi, I have a scrollview which currently displays a multi-line (70+) plain text file which doesn't look very good. Is it possible to embed the tokens (e.g. {U}) within the plain text file and then use the RichStringFormatter to display the underlined text (e.g. "This is {U}underlined{U})? I'm not yet familar with the RichString library and the answer might be there. If it can be done, does anyone have a example of reading a text file with embedded tokens to share? Thanks in advance!
 
Top