B4J Question [B4X] BCTextEngine - Background color to a piece of text is possible ??

Status
Not open for further replies.

Erel

B4X founder
Staff member
Licensed User
Longtime User
(The screenshot doesn't show anything. Better to remove it)

There is no built-in support for background colors, but it is possible with some work:

rxQbB4ql5h.gif


1. You need to disable lazy loading in the designer.
2.
Process_Globals:
B4X:
Private WordsToHighlight As B4XSet
3.
B4X:
WordsToHighlight = B4XCollections.CreateSet2(Array("AAAA", "eflkw"))
    BBCodeView1.Text = $"fk we;lfk ;lwe kf;lw elk;fw ef
wefjlk wejflkw ejlkfwj [plain]eflkw[/plain] efjlkwe fjklwe f

fjskldfj sldkfj slkdf jslkdf jlksdjf lksdjf lksd fjlskd fjlksdf jlskdf  sdf sdlkfj skldf [plain]AAAA[/plain] sdf sd
fsd
fs
dfs jdklflsj dlfkjs dlfk sjdlfk  "$
    AddBackgroundPanels
End Sub

Sub MainForm_Resize (Width As Double, Height As Double)
    AddBackgroundPanels
End Sub
Private Sub AddBackgroundPanels
    For Each x As B4XView In BBCodeView1.sv.ScrollViewInnerPanel.GetAllViewsRecursive
        If x.Tag = "background" Then
            x.RemoveViewFromParent
        End If
    Next
    Dim scale As Float = TextEngine.mScale
    For Each line As BCTextLine In BBCodeView1.Paragraph.TextLines
        For Each un As BCUnbreakableText In line.Unbreakables
            For Each s As BCSingleStyleSection In un.SingleStyleSections
                If WordsToHighlight.Contains(s.Run.Text) Then
                    Dim pnl As B4XView = xui.CreatePanel("")
                    pnl.Color = 0xFF00D2FF
                    pnl.Tag = "background"
                    BBCodeView1.sv.ScrollViewInnerPanel.AddView(pnl, BBCodeView1.Padding.Left + s.AbsoluteStartX / scale - 2dip, _
                     BBCodeView1.Padding.Top + (line.BaselineY - s.MaxHeightAboveBaseLine) / scale - 2dip, _
                        s.Width / scale + 4dip, (s.MaxHeightBelowBaseLine + s.MaxHeightAboveBaseLine) / scale + 4dip)
                    pnl.SendToBack
                End If
            Next
        Next
    Next
End Sub
 
Upvote 0

Juan Perz

Member
Hello,
The first AAAA and the last AAAA should not have a Background Color, or am I doing something wrong?
The AAAA from the medium comes out correctly.

1596065811639.png

B4X:
    WordsToHighlight = B4XCollections.CreateSet2(Array("AAAA", "eflkw"))
    BBCodeView1.Text = $"AAAA[plain]eflkw[/plain]AAAA fk we;lfk ;lwe kf;lw elk;fw ef
wefjlk wejflkw ejlkfwj [plain]eflkw[/plain] efjlkwe fjklwe f

fjskldfj sldkfj slkdf jslkdf jlksdjf lksdjf lksd fjlskd fjlksdf jlskdf  sdf [b]sdlkfj[/b] skldf AAAA sdf sd
fsd
fs
dfs jdklflsj dlfkjs dlfk sjdlfk  [plain]AAAA[/plain]dfg[b]sdlkfj[/b]AAAA"$
    AddBackgroundPanels
End Sub


Sub MainForm_Resize (Width As Double, Height As Double)
    AddBackgroundPanels
End Sub

Private Sub AddBackgroundPanels
    For Each x As B4XView In BBCodeView1.sv.ScrollViewInnerPanel.GetAllViewsRecursive
        If x.Tag = "background" Then
            x.RemoveViewFromParent
        End If
    Next
    Dim scale As Float = TextEngine.mScale
    For Each line As BCTextLine In BBCodeView1.Paragraph.TextLines
        For Each un As BCUnbreakableText In line.Unbreakables
            For Each s As BCSingleStyleSection In un.SingleStyleSections
                If WordsToHighlight.Contains(s.Run.Text) Then
                    Dim pnl As B4XView = xui.CreatePanel("")
                    pnl.Color = 0xFF00D2FF
                    pnl.Tag = "background"
                    BBCodeView1.sv.ScrollViewInnerPanel.AddView(pnl, BBCodeView1.Padding.Left + s.AbsoluteStartX / scale - 2dip, _
                     BBCodeView1.Padding.Top + (line.BaselineY - s.MaxHeightAboveBaseLine) / scale - 2dip, _
                        s.Width / scale + 4dip, (s.MaxHeightBelowBaseLine + s.MaxHeightAboveBaseLine) / scale + 4dip)
                    pnl.SendToBack
                End If
            Next
        Next
    Next
End Sub
 
Last edited:
Upvote 0
Status
Not open for further replies.
Top