Android Question CSBuilder

adriano.freitas

Active Member
Hi! The use of CSBuilder is quite interesting, however, I have a question: If I have an edittext that already has a text. How do I colorize or Bold just one word from existing text? Or is it only possible to APPEND, having to separate the text into several parts and join it again with append?

Thanks
 

adriano.freitas

Active Member
You will need to replace the complete text with CSBuilder.

This might help: https://www.b4x.com/android/forum/threads/csbuilder-marking-based-on-regex-pattern.83002/#content
Hi! Thank you very much for your attention and information! I'll try to work with it, but looking at it quickly, the impression I have is that it won't help me. In fact I wanted to use an EditText as a base to create an object that allowed the input/display of data with basic types of formatting (bold, italic, underline and font color). For that I would need to take what was marked by the user and apply a formatting that could later be changed. It should also be able to take all the formatted text, save it and return with it. But it seems to me that this is not possible. Is there any object or library that does this in a practical way? I saw some but they used a lot of external dependencies and ended up very vulnerable, not to mention that some (already old) didn't even work well here....
 
Upvote 0

Quandalle

Member
Licensed User
You can have look at TagCsBuilder a class that I had made some time ago. TagCSBuilder is a class built on top of CSBuilder (source code include, and no external dependencies).
With TagCSbuilder the formatting information is not provided by method calls but by tags included in a string.
This makes it easier to modify the formatting by changing the tags or save/restore : it's string manipulation

https://www.b4x.com/android/forum/threads/charsequence-tagcsbuilder.127015/
 
Upvote 0

adriano.freitas

Active Member
You can have look at TagCsBuilder a class that I had made some time ago. TagCSBuilder is a class built on top of CSBuilder (source code include, and no external dependencies).
With TagCSbuilder the formatting information is not provided by method calls but by tags included in a string.
This makes it easier to modify the formatting by changing the tags or save/restore : it's string manipulation

https://www.b4x.com/android/forum/threads/charsequence-tagcsbuilder.127015/
Hi!
Very good! A fantastic code! It helped me a lot for what I need. It allowed me to make additions so that when defining a text box, you can mark a word or text snippet and format it as desired. Almost a text editor! (after fully ready I can share).

However, I still have a serious problem that without solving it is useless: Imagine that I have a text in the text box that is formatted (with colors, styles and without tags, as it is already the processed content). If the person adds some word in it, I can't get the new text (previous formatting with new word).

When the person formats the text, I keep a copy of it with the tags in a variable, but I have no way to "mirror" what is being typed in the text box in this variable with the tags, as the positions vary a lot and it would be very complex this processing. Do you have any idea, suggestion?
 
Upvote 0

Quandalle

Member
Licensed User
A scenario is for each character :
  • read the content of the textbox
  • read the cursor position
  • reintegrate the tags according to the previous version (simplified example below : BuildNewTagString )
  • write the textbox the new text with formatting tag
  • Reposition the cursor in the textbox
B4X:
Sub BuildNewTagString (oldTagS As String, newS As String) As String

Dim inTag As Boolean = False
Dim ioldTagS As Int = 0
Dim inewS As Int = 0
Dim res As StringBuilder :     res.Initialize
Dim lenghtChange  As Int = Regex.Replace("<.*?>",oldTagS,"").Length - newS.Length

Do While ioldTagS < oldTagS.Length
        If oldTagS.CharAt(ioldTagS) = "<" Then
            inTag = True
            res.Append(oldTagS.CharAt(ioldTagS))
        Else
            If inTag Then             
                res.Append(oldTagS.CharAt(ioldTagS))
                If oldTagS.CharAt(ioldTagS) = ">" Then inTag = False
            Else
                If oldTagS.CharAt(ioldTagS) = newS.CharAt(inewS) Then
                    res.Append(oldTagS.CharAt(ioldTagS))
                    inewS = inewS +1
                Else
                    Select lenghtChange
                        Case -1  ' Char have been added
                            res.Append(newS.CharAt(inewS))
                            inewS = inewS +1
                            ioldTagS = ioldTagS -1
                        Case 0 ' char have been changed
                            res.Append(newS.CharAt(inewS))
                            inewS = inewS +1
                        Case 1 ' char have been removed
                    End Select           
                End If       
            End If
        End If
        ioldTagS = ioldTagS+1   
Loop
Return res.ToString
End Sub

This allows to treat simple cases, in the other cases it would be necessary to use a complete editor with possibility of formatting
 
Upvote 0
Top