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,396
Last edited by a moderator:

brainfart

Member
Licensed User
Longtime User
Feature request. (If possible naturally):sign0147:
Add method to load RTF (rich text format) file and pass the formatting to a multi-line label.
I am trying to write a help file/user manual and embed it in my application.
The easiest way to write and then maintain a user manual with bold headings, underlines and italics would be to use word editing tool and saving it to file, then adding it to resources in the app.
I am trying to avoid using WebView because of INTERNET permission which turns off a lot of potential down-loaders before they read fine print from me explaining that I do not use their data plan.

Thanks.
 
Last edited:

agraham

Expert
Licensed User
Longtime User
Add method to load RTF (rich text format) file and pass the formatting to a multi-line label.
There's no point in putting that in a library, it could just as well be implemented in a Code module with no loss of performance and more flexibilty in being able to modify it. I leave the challenge of the implementation to you. :)
 

reeZZer

Member
Licensed User
Longtime User
Hello agraham,

thank you very much for his excellent Library , my Chat has now COLORS :sign0060:

But i see that this only works for Labels and EditText i think, would it be possible to change the color of a specific Text in a Listview?

I show the Userlist over Liestview and would like to color specific names like this:
rs.Initialize("{U}Username{U}")
rs.Color2(Colors.Blue, "{U}")
UserList.AddSingleLine(rs)

but that don't work , any other format is the same result.

Thank in advance !

Keep up the good work.

greeTz,
reeZZer
 

quimacama

Member
Licensed User
Longtime User
I can't show more than one superscript?

I have this code

Dim rs As RichString
Dim rsb As RichStringBuilder

rsb.Initialize
rsb.Append ("(2{superscript}3{superscript}) ")
rsb.Append ("(3{superscript}2{superscript}) ")
rsb.Append ("(5{superscript}4{superscript}) ")
rs.Initialize(rsb)
rs.Superscript2( "{superscript}")
label.Text= rs

but the result in the label is (2³)*(3{superscript}2)*(54{superscript})


What can I do?
 

agraham

Expert
Licensed User
Longtime User
Shorten your tokens
.
B4X:
rsb.Initialize 
rsb.Append ("(2{SUP}3{SUP}) ")
rsb.Append ("(3{SUP}2{SUP}) ")
rsb.Append ("(5{SUP}4{SUP}) ")
rs.Initialize(rsb) 
rs.Superscript2( "{SUP}")
It is failing because you are superscripting a single character and the token recognition is failing. Longer superscripted sequences will work with longer tokens. Unfortunately I can't immediately see why this happens but it looks like the length of the token must be less than (length of the formatted span + 5)
 

peacemaker

Expert
Licensed User
Longtime User
There's no point in putting that in a library, it could just as well be implemented in a Code module with no loss of performance and more flexibilty in being able to modify it. I leave the challenge of the implementation to you. :)

Does it mean to parse RTF file format, re-formatting loaded text by RichString lib line-by-line ... ?
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Hi agraham,
may thanks for your useful library.
Please, can you help me? I want to set this code:

rs.Initialize("{BI}{C}{U}COMMESSA{U}{C}{BI}")
rs.Color2( Colors.Cyan, "{C}" )
rs.Underscore2("{U}")
rs.Style2( rs.STYLE_BOLD, "{BI}" )

to other 10 label.text, changing "COMMESSA" with other.
Who can I do, without re-write the same code all times?
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Something like:
B4X:
Sub CreateRS(Title As String) As RichString
rs.Initialize("{BI}{C}{U}" & Title & "{U}{C}{BI}")
rs.Color2( Colors.Cyan, "{C}" )
rs.Underscore2("{U}")
rs.Style2( rs.STYLE_BOLD, "{BI}" )
return rs
End Sub

Label1.Text = CreateRS("COMMESSB")

Hi Erel,
like a function? I've just tried it, but without success
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
I did it. Many thanks!!! :sign0188:
:sign0098:
 

Discorez

Member
Licensed User
Longtime User
Erel, it is possible to show the caret for colorized text, if RichString used with EditText?
 

Discorez

Member
Licensed User
Longtime User
For example, as code editor with syntax highlighting, or display errors for check spelling. The Rich text also should "participate" in editing - the caret and selection color, copy/paste, e.t.c.
It is possible to create better an independent class - RichEditText?
 

mjtaryan

Active Member
Licensed User
Longtime User
More brainstorming...


Might it not be possible to create a "library" of keyboard symbols (or combinations of symbols) that represent certain things? I'm expanding from the {R} in an earlier post. For example ^This is Bold^, !This is Italic! and ~This is Underline~ where combining the enclosing symbols (i.e. ^~) would likewise combine the effect. Perhaps I'm taking out of turn because I haven't yet looked at the library. Also, it would be nice to be able to load and display a pre-formatted RTF file.
 
Top