Android Question How create CsBuilder from text sequence

netsistemas

Active Member
Licensed User
Longtime User
something have a builder like this message:


Create a CsBuilder From text.
'cs.initialize2("This is a {Red}RED{Red} word and this is {B}Bold{B}")
 
Last edited:

netsistemas

Active Member
Licensed User
Longtime User
my first aproximation:

copy code and execute:
B4X:
GETCSBuilderDemo

'or simila t
MsgboxAsync(GetCsBuilderText("#Color.GREEN##FONTSIZE.32##TYPEFACE.FONTAWESOME.0XF0E7##TYPEFACE.DEFAULT##FONTSIZE.16#Recibido   (A) Nº:78462 / Partida:2 #FONTSIZE.16##COLOR.YELLOW#F.entrega: 2022-06-14 CONDUCTO CHAPA INJERTO CIRCULAR Ø 100 en conducto Ø 250"),"hellO")

B4X:
public  Sub GetCsBuilderText(Texto As String ) As CSBuilder
    Dim CS As CSBuilder
    Dim S As String
    Try
        'FIRST SPLIT MUST BE A TEXT .   ********************************************** DELETE FIRST CHAR DOWN   #ESPECIAL CODE 1#
        Texto = " " & Texto
        Dim M() As String
      
        M = Regex.Split("#",Texto)
        'SAMPLE:
        'HOLA, #RED#AMIGOS DEL #YELLOW##FONTSIZE25# MUNDO MUNDIAL
      
      
        CS.Initialize

        For f = 0 To M.Length-1
            s = M(f)
            If F = 0 Then   '**********************************************************DELETE DE FIRST CHAR ADD   #ESPECIAL CODE 1#
                S = S.SubString(1)
            End If
            If f Mod 2 = 1 Then
          

          
                Select Case S.ToUpperCase
                    Case  "COLOR.RED"
                        CS.Color(Colors.Red)
                    Case  "COLOR.YELLOW"
                        CS.Color(Colors.Yellow)
                    Case  "COLOR.GREEN"
                        CS.Color(Colors.Green )
                    Case  "COLOR.MAGENTA"
                        CS.Color(Colors.Magenta )
                    Case  "COLOR.BLACK"
                        CS.Color(Colors.Black)
                    Case  "COLOR.White"
                        CS.Color(Colors.White )
                    Case  "COLOR.CYAN"
                        CS.Color(Colors.Cyan )
                    Case  "COLOR.DarkGray"
                        CS.Color(Colors.DarkGray)
                    Case  "COLOR.Gray"
                        CS.Color(Colors.Gray)
                    Case  "COLOR.LightGray"
                        CS.Color(Colors.LightGray)
                    Case  "COLOR.Transparent"
                        CS.Color(Colors.Transparent)
                    Case "TYPEFACE.DEFAULT"
                        CS.Typeface(Typeface.DEFAULT )
                  
                        '... MORE COLOR
              
                        'FUTURE IMPLEMETATIONS:**********************************************
                    Case Else
                        If S.StartsWith("TYPEFACE.") Or S.StartsWith("FONTSIZE.") Then
                            'Log(S)
                            Dim UltimoPunto As Int
                            Dim ValorTrasUltimoPunto0X As String
                            UltimoPunto = S.LastIndexOf(".")
                            Dim ValorTrasUltimoPunto As String
                            Dim ValorPrevioUltimoPunto As String
                            ValorTrasUltimoPunto = S.SubString(UltimoPunto+1)
                            ValorPrevioUltimoPunto = S.SubString2(0,UltimoPunto)
                          
                            Dim ii As Char
'                            Chr(0xF0E7)
                          
                            Dim ValorInt As Int
                            'SOLO PARA FONTAWESOME Y MATERIALICONOS
                            If  S.StartsWith("TYPEFACE.FONTAWESOME") Or S.StartsWith("TYPEFACE.MATERIALICONS") Then
                                If ValorTrasUltimoPunto.Length > 2 Then
                                    ValorTrasUltimoPunto0X = ValorTrasUltimoPunto.SubString(2)
                                    ValorInt = Bit.ParseInt(ValorTrasUltimoPunto0X,16)
                                    ii  = Chr(  ValorInt )
                                End If
                            Else
                                If ValorPrevioUltimoPunto = "TYPEFACE" Then   'TYPEFECE.LOQUESEA   (Y SIN MAS VALORES)
                                    ValorPrevioUltimoPunto = S
                                End If
                                ValorTrasUltimoPunto0X = ""
                                ValorInt= 0
                            End If
                          
                          
                            Select Case ValorPrevioUltimoPunto
                                Case "TYPEFACE.FONTAWESOME"
                                    CS.Typeface(Typeface.FONTAWESOME)
                                    CS.Append(ii)
                                Case  "TYPEFACE.MATERIALICONS"
                                    CS.Typeface(Typeface.FONTAWESOME)
                                    CS.Append(ii)
                                Case "TYPEFACE.DEFAULT"
                                    CS.Typeface(Typeface.DEFAULT)
                                Case "TYPEFACE.DEFAULTBOLD"
                                    CS.Typeface(Typeface.DEFAULT_BOLD)
                                Case "FONTSIZE"
                                    ValorInt = Bit.ParseInt(ValorTrasUltimoPunto,10) 'BASE 10
                                     CS.Size(ValorInt)
                            End Select
                          
                        End If
                  
                  
                  
                  

              
                      
                End Select
            Else
                CS.Append(S)
            End If




        Next
        CS.PopAll
  
        Return CS

    Catch
        Log(LastException)
        Return CS.Initialize.Append("Error: " & S).PopAll
    End Try


End Sub
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
Have you seen RichStringFormatter?
 
Upvote 0

netsistemas

Active Member
Licensed User
Longtime User
But in customlistview, i prefer use csbuilder, (but IMPORTANT... this will no run in b4j and Ios. i only develop for android)
Edit new version of my function in second post
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Here is a starting point with regular expressions.
 
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

Similar Threads

Top