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

Turbo3

Active Member
Licensed User
Longtime User
If I throw in a DoEvents into the loop that reads in the Help file I can get it to work but only the first time. After that the code skip reading the help file and just uses the already setup rs variable. Then I needed to throw in DoEvents all over to try to get the dialog to show. When I do that I get a black screen for a while which never happened before. Not sure I like that.

The problem is trying to determine where to put the DoEvents in the code. It is just trial and error right now. Not the best way to code.
 

Dave O

Well-Known Member
Licensed User
Longtime User
Great library - lets me highlight text while still using standard views (e.g. labels). Thanks!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
V1.5 - New methods: RichString.CustomTypeface / CustomTypeface2. These methods are similar to Typeface / Typeface2 but allow to pass a Typeface object instead of passing the typeface name.
This allows using custom fonts (such as FontAwesome) with RichString.

Example:
B4X:
Dim rs As RichString
rs.Initialize("FontAwesome + Material Icons: FA" & Chr(0xF17B) & "FA MI" & Chr(0xEB3B) & "MI")
rs.TypefaceCustom2(lblFontAwesome.Typeface, "FA") 'lblFontAwesome was added with the designer and its typeface is set to FontAwesome
rs.TypefaceCustom2(lblMaterialIcons.Typeface, "MI") 'lblMaterialIcons - same thing with Material Icons.
Label1.Text = rs

SS-2017-01-05_17.13.08.png
 

Turbo3

Active Member
Licensed User
Longtime User
Can you add the ability to select a custom font to the RichStringFormatter too?

I need to be able to replace the "monospace" font with a real monospace ttf font.
B4X:
rsf.AddToken("{m}", rsf.FORMAT_TYPEFACE, "monospace")
I would like to use "UbuntuMono-R.ttf" instead.
 

Turbo3

Active Member
Licensed User
Longtime User
That support is for RichString type. I am requesting for RichStirngsFormatter type. I need to be able to add a token that will switch the font.
 
Last edited:

Turbo3

Active Member
Licensed User
Longtime User
I am going to need to write a small sample app as when I try this with my full app the external font is not being used by the RichStringFormatter.

Seems like the AddToken(xxx, FORMAT_TYPEFACE, "mono") statement is being ignored and not using the new "mono" font.
 

Turbo3

Active Member
Licensed User
Longtime User
I need support for embedded tokens. So if it supports those including typeface switching then it would fit my needs.
 

Turbo3

Active Member
Licensed User
Longtime User
As far as I can tell the following code does not define a new font that the AddToken/FORMAT_TYPEFACE function can access using the tag "mono".

B4X:
rs.TypefaceCustom2(Typeface.LoadFromAssets("UbuntuMono-R.ttf"), "mono")
...
rsf.AddToken("{m}", rsf.FORMAT_TYPEFACE,"mono")
 

Turbo3

Active Member
Licensed User
Longtime User
For embedded tags Erel said to use RichStrings not CharSequenceBuilder. (from Feb 2017)


I have lots of "token encoded" full texts and applications that will be hard to convert to csbuilder.
You can still use RichString library.
 

Turbo3

Active Member
Licensed User
Longtime User
The key thing I need is support for embedded formatting tags including switching between normal and custom fonts all in the same string. The string can be tens of thousands of characters long or more.

I get this today with the RichStringFormatter except for the switching of fonts. Since font switching was added to RichStrings it seemed reasonable to request it for the RichStringFormatter.

Using CSBuilder means a complete rewrite of all the code in that area once I understand how to make it work.
 

Turbo3

Active Member
Licensed User
Longtime User
So we have come full circle from "it can already be done" to " never be updated".

I have added a little code so my embedded tags can include external font changes with no changes to my original source data (I use {m} to select a monospace font). All is good with just a minor updated to my app.

The original problem was that the default Android "monospace" font on most Android devices uses a "bispace" font (two sizes). One width for characters (A-Z 0-9) and a larger one for line drawing characters. This defeats one of the uses of a monospace font. With this new extension I can now switch to a real monospace font when needed mixing line drawing with normal text. Interesting that Apple has no such problem understanding what a monospace font should look like.
 

Star-Dust

Expert
Licensed User
Longtime User
I suggest switching to CharSequenceBuilder as the RichString Library not getting any updates since years (Author inactive)
@agraham still accesses the forum to today. I also read his post.
Do you Think that does not develop anymore?
 
Top