B4i Library [class] RichString

Edit: don't use this class. Better to use CSBuilder or BCTextEngine.

SS-2014-12-24_16.08.56.png


The RichString class uses NativeObject to create attributed strings. Similar to B4A RichString library.

For example the text above is created with the following code:
B4X:
Private Sub Page1_Resize(Width As Int, Height As Int)
   Dim rs As RichString
   rs.Initialize(Label1.Text)
   rs.Color(Colors.Red, 0, 4).Color(Colors.Blue, 4, 8).SetFont(Font.CreateNewBold(30), 1, 5)
   rs.Strikethrough(True, 5, 10)
   rs.Underline(True, Colors.Blue, 0, 10)
   rs.SetFont(Font.CreateNewItalic(20), 0, rs.Text.Length-5)
   rs.SetFont(Font.CreateNewBold(30), 4, 7)
   rs.BackgroundColor(0xFFC0C000, 5, 20)
   rs.SetToLabel(Label1)
End Sub

The range of each attribute is made of two parameters: start index (inclusive) and end index (exclusive).

Note that if the label was added with the designer then the font size will be reset whenever AutoScaleAll is applied. In that case you need to set it again in Page_Resize event.
 

Attachments

  • RichString.zip
    3.1 KB · Views: 436
Last edited:

Hugh Thomas

Member
Licensed User
Longtime User
Thanks.

A couple more I need to complete porting my app. How do I set Bold and Italics? I punted on NSBold and NSItalic but they didn't work.

Also, how can I set the font size? I tried using rich.SetFont passing it a font created with fnt.CreateNew(size) but it doesn't seem to have any effect.

Thanks,

Hugh
 

Hugh Thomas

Member
Licensed User
Longtime User
Sorry, should have reread the whole thread. I've got Bold and Italic working now.

The problem with the text size not working wasn't because it was created with the designer - all my UI is created programatically.

I eventually tracked down the problem to a call to lbl.AdjustFontSizeToFit, which I did after I'd assigned the rich string to the label with rs.SettoLabel(lbl). I thought this would adjust the size of the formatted text, but it looks like it removes some of the formatting.

I found if I call lbl.AdjustFontSizeToFit *before* I do the rs.SettoLabel(lbl) then it correctly adjusts the size of the formatted text.

Hugh
 

Turbo3

Active Member
Licensed User
Longtime User
Any chance of adding richstringformater type so we can just define tokens then apply to a richstring with rs=rsf.format(rs)
 

Hugh Thomas

Member
Licensed User
Longtime User
I've attached a sample app using a routine I wrote to do token-based rich string formatting. It is by no means a general purpose library or class; I wrote it as an interim measure while porting my Android app to iOS. As such it has a hard coded list of tokens it will accept, but it's a simple matter to add/change the tokens so it may be of use until a proper library or class comes along.

To add a new token you need to do two things:

1) Add the token to the list of valid tokens. For example, to add a "<yellow>" token:
B4X:
Private tokens()     As String = Array As String ("<yellow>", "<b>", "<i>", "<st>", "<bt>", "<ss>", "<ul>", "<red>")
2) Add a Case for this token to the select statement in Sub FormatRichString:
B4X:
    Case "<yellow>"
        rich.Color(Colors.Yellow, p1, p2)

Please note that this example includes Erel's RichString class, but I've updated it to include rich.SuperScript()

I hope this is of some use.

Hugh

NOTE: Attached ZIP updated to V0.2 with change to handle any tag, not just tags starting with "<".
 

Attachments

  • RichText0.2.zip
    61.8 KB · Views: 52
Last edited:

fbritop

Active Member
Licensed User
Longtime User
Is B4A RichString library source code available, so this class (B4I) can be modified to work with tokens?

Thanks
FBP
 

AllanH

Member
Licensed User
Longtime User
For anyone coming here for substrings, here is how to add to the RichString codebase
For some reason, while the attribute "NSSuperScript" works, "NSSubstring" does not.
You need to use the following code and unfortunately pass the fontsize (superscript is half the font size shifted up, so I did the same shift size for Subscript)

B4X:
Public Sub SubScript(Start As Int, EndIndex As Int, TextSize As Double) As RichString
   Return AddAttribute("NSBaselineOffset", TextSize/-2, Start, EndIndex)
End Sub
 
Top