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

mjtaryan

Active Member
Licensed User
Longtime User
...I will try another approach tomorrow.

Sorry.

OK. See if the attached project displays the log entries like you want -- that is with formatting.

Study the source code with its comments and compile it to your phone. It will save the log entries to the root directory of your sd card ("Log.txt").

The Main activity allows you to capture the time and then format and display it. The "List" button displays the second activity which merely displays the contents of the saved file. The Main activity uses a Label in a ScrollView because it is to be read only (non-editable). The second activity uses an EditText view to demonstrate that the formatting works there as well.

What I discovered as I worked on this is that the formatted text (i.e. "rs") must be directly assigned to the Text property of the View that will hold it. You CANNOT first assign the formatted text to an intermediate variable (i.e. OutText = rs) with the intention of assigning the contents of OutText to the display view later. Doing so will strip the tokens from the text. There is more, but that appears to be the most salient caveat with respect to using the RichString formatter.

I'll try to answer any question about the test app, but as for going further with this, I'm afraid I've run out of time.

Mike
 

Attachments

  • Log Test.zip
    390.2 KB · Views: 356

Ionut Indigo

Member
Licensed User
Longtime User
Is there a way to change the horizontal alignment from RichString? I need that for a ScrollView, where some lines of text start from the right and others from the left. The only way to resolve that is to add multiple Labels with different horizontal aligment?
 

Bryan

Member
Licensed User
Longtime 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.

This is kind of what I was looking for. A way to change the size of text in a Widget Label. I would like an opinion if this can be made to work with code provide by Klaus for Auto sizing the text for a label. I know his code cannot be used directly for this. Can it be done somehow similar to the way his code does it but for a label used in a widget.
I'm not too sure how to go about it yet, and don't want to waste a lot of time trying to get it to work if it actually can't be done.
Thanks,

B4X:
Sub Globals
  Dim stu As StringUtils
End Sub

Sub SetTextSize(lbl As Label, txt As String)
  Dim dt As Float
  Dim limit = 0.5 As Float
  Dim h As Int

  lbl.Text = txt
  lbl.TextSize = 72
  dt = lbl.TextSize
  h = stu.MeasureMultilineTextHeight(lbl, txt)
  Do While dt > limit OR h > lbl.Height
    dt = dt / 2
    h = stu.MeasureMultilineTextHeight(lbl, txt)
    If h > lbl.Height Then
      lbl.TextSize = lbl.TextSize - dt
    Else
      lbl.TextSize = lbl.TextSize + dt
    End If
  Loop
End Sub

Code was provided by Klaus
 

Bryan

Member
Licensed User
Longtime User
Why can't you use RemoteViews.TextSize to set the text size?

You are right, I was looking at using that but then I needed a way to retrieve the text size "dt" (as in Klaus's code) and remoteViews does not provide for that I think.

Is there a way to retrieve text width from a remote view label's text?
 
Last edited:

a2stepper

Member
Licensed User
Longtime User
Very useful library. is there a way to use to put different colored text into a listview or scrollview??
when i try, the color does not display, just black letters.
 

Mousa Najafi

Member
Licensed User
Longtime User
Hi
Is it possible to use custom fonts in richstring? for example the font that is created from font file like following code:
B4X:
Typeface.LoadFromAssets("lotus.ttf")
Always I have a problem to set custom typeface in cases like activity.title or inputlists etc. that has not typeface property.
 

Turbo3

Active Member
Licensed User
Longtime User
Is there any way to save a richstring to a file and then reload the richstring again?

I use richstrings for my help file which is 90KB so it takes many (>5) seconds even using rsf.format. For now I play a sound during this time so the user does not think the app is hung when they try to view the Help file.

I did change the app so it only does this once as long as the app is not killed by making rs global and setting a flag when rs has be loaded so the delay is normally only seen once. But it would be nice just to pre-process the tokens and then save it in a file. Then when needed I just load the rs file into a rs variable and assign it to a label.
 

chrjak

Active Member
Licensed User
Longtime User
Is there any way to save a richstring to a file and then reload the richstring again?

I use richstrings for my help file which is 90KB so it takes many (>5) seconds even using rsf.format. For now I play a sound during this time so the user does not think the app is hung when they try to view the Help file.

I did change the app so it only does this once as long as the app is not killed by making rs global and setting a flag when rs has be loaded so the delay is normally only seen once. But it would be nice just to pre-process the tokens and then save it in a file. Then when needed I just load the rs file into a rs variable and assign it to a label.
Not sure that i understand... But normally you can write everything to a file... So i think this is possible

Well another tip at the end: Personally i would remove the sound... Sound can be really annoying if you don't want to hear some sound :D Make something graphical instead ;)
 

Turbo3

Active Member
Licensed User
Longtime User
Yes I can write a richstring out but it strips off all the internal tags so it is just a plain string in the text file. I want to write out the full contents of the rs variable so I can read it directly back into a richstring type variable. Don't confuse this with a plain text file with tags added (things like {Green}) that needs to be processed into a richstring. For this I think I will need a new File.WirteRS to use instead of File.WriteString.

As for replacing sound with a graphic I have tried using the ToastMessage but it does not show up until after the Help file is displayed so useless. The fact that it plays only once unless you go to the Android Settings/Apps screen and kill the app makes it far less annoying. The sound I use will probably have some people killing the app just to hear it again. ;-)
 

chrjak

Active Member
Licensed User
Longtime User
For a graphical sign you can use a progressbar in indeterminate mode or progressdialogshow(...)

If it helped i would be happy about a like ;D
 

Turbo3

Active Member
Licensed User
Longtime User
progressdialogshow()does not show up on the screen until after the help is loaded. So no value. I have no idea why it is delayed but the delay makes it useless. Only sound kicks off right away when I call the display_Help routine. Help is displayed on a scrollview which is created when help is called but I tried delaying setting up the scrollview until after most of the processing with no effect.
 
Top