B4A Library RichString library.

Discussion in 'Additional libraries, classes and official updates' started by agraham, Aug 6, 2011.

  1. agraham

    agraham Expert Licensed 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.

    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

    Attached Files:

    Last edited by a moderator: Jan 5, 2017
    AnandGupta, KZero, fredo and 4 others like this.
  2. Kamac

    Kamac Active Member Licensed User


    is actually hard to perform, because we have to count our letters to place the colored word. It takes time, and is very hard to do with dynamic text.

    This solution:

    would be way better, almost the same as:

  3. margret

    margret Well-Known Member Licensed User

    Rich text

    Hello, What about doing something like this as an idea:

    rs.Initialize("This is not a RichString")
    rs.Color(Colors.Red, "Rich", "String")

    Do the equivalent to At(String) As INT, this way you don't have to count, it will be the same as: rs.Color(Colors.Red, 14, 18). Just a thought.


    TheGiant likes this.
  4. agraham

    agraham Expert Licensed User

    I think this better written as a utility method in a code module if you want it as it can easily be implemented using the core functionality of the library.
  5. Erel

    Erel Administrator Staff Member Licensed User

    I like the XML/Html variant. I suggest to make the syntax as short as possible (<b>, <u>).
    You will need to somehow allow writing '<' though. Maybe with an additional Initialize method.
  6. agraham

    agraham Expert Licensed User

    You could do something like that anyway with

    rs.Initialize("This <u>is<u> not a RichString")

    Which I implemented now anyway as it was dead easy. The real decision is to whether to stick to

    rs.Initialize("This {R}is{R} not a {R}Rich{R}String")
    rs.Color2(Colors.Red, "{R}")

    or also implement

    rs.Initialize("This <col 0xffff0000>is</col> not a <col 0xffff0000>Rich</col>String")
    rs.Color2(Colors.Red, "{R}")

    Which gets into XML parsing territory and is also possibly not as easy to use programmatically on dynamic strings. Thoughts?
  7. margret

    margret Well-Known Member Licensed User

    Rich String

    Sorry for the previous suggestion. When I saw Erel's post, it just hit me about what you were doing. I think Erel's suggestion is best also. I much prefer this one:

    rs.Initialize("This {R}is{R} not a {R}Rich{R}String")
    rs.Color2(Colors.Red, "{R}")


    Last edited: Aug 6, 2011
  8. agraham

    agraham Expert Licensed User

    Well that was all a lot easier than I thought. Version 1.1 now posted will format multiple regions in a RichString using programmer defined metacharacter tags.
  9. ZJP

    ZJP Active Member Licensed User

    rs.Initialize("This is not a RichString")
    Colors.Red, 1418)

    Edit : Tested. I like this lib. Thx. ;)
    Last edited: Aug 6, 2011
  10. myriaddev

    myriaddev Active Member Licensed User

    Wow! Thanks!

    Neat! Already being used!
    Thanks, Jerry
    siroky likes this.
  11. margret

    margret Well-Known Member Licensed User

    RichString Library

    Very, very Nice, many uses for this!! Thank you!


  12. moster67

    moster67 Expert Licensed User

    Very nice Agraham. :sign0098:

    For labels, it is perfect since mostly the text remains "static" and the text is populated by code.

    Just for the fun of it, I tried it also in an EditText-view and the richtext-formatting works nicely there as well. It just gets tricky when you let the user insert and modify text but perhaps using the events of EditText, maybe one can come up with a solution. I will play with it later again and see if I can make a small notepad-application with Richtext-properties.

    As always, thanks for your great work.
    Markos likes this.
  13. agraham

    agraham Expert Licensed User

    Version 1.2 now posted includes the RichStringFormatter object. I'm quite pleased with this.

    RichStringFormatter lets you define your formatting tokens once and you can then use it time and time again to format RichStrings by just passing a RichString to it. This should make it much easier to program RichStrings whose formatting may be dynamically changed at runtime.
  14. Djembefola

    Djembefola Active Member Licensed User

    Great Library

    Thank you.

  15. agraham

    agraham Expert Licensed User

    Version 1.3 now posted has a couple of improvements.

    Previously if you used an undefined token in the RichString passed to a RichStringFormatter your application would force close because the exception was raised on the rendering thread and not the main thread so it could not be caught. Now an explicit check is made for this and a new UnimplementedToken method will return an array of any such encountered while formatting.

    A new format option, ScaleX, has been added as I overlooked it before. It expands or compresses text horizontally.
    Last edited: Aug 9, 2011
  16. corwin42

    corwin42 Expert Licensed User

    I tried to use RichString formatting in a homescreen widget label but it does not work.

    Am I right that it is because RemoteView.SetText() has a String type as second parameter so if I pass a RichString it will be converted to a normal String?

    I think I currently have no chance to get it working?
  17. agraham

    agraham Expert Licensed User

    The String type of the Text parameter will indeed be the problem. The underlying Android setTextViewText method does in fact take a CharacterSequence type so it could probably be made to work with the Reflection library. If you post a small project with a widget with a label showing your present attempt I'll see if I can modify it to work.
  18. corwin42

    corwin42 Expert Licensed User

    Ok, I hacked down a simple example. The label should show the text in two colors.

    Attached Files:

  19. agraham

    agraham Expert Licensed User

    Here you are. It's an inline demo, for efficiency you will probably want to refactor it into a Sub and have global RichTextFormatter instance etc. etc.

    Attached Files:

  20. corwin42

    corwin42 Expert Licensed User

    Thanks very much, but...

    Compiling code.                         Error
    Error compiling program.
    Error description: Too many parameters.
    Occurred on line: 
    1) = rsf.Format(rs, rsf.SPAN_INCLUSIVE_EXCLUSIVE) ' CharSequence
    Word: rsf
    New version of RichString Library?
  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