B4J Question [BCTextEngine] BBLabel WordWrap does not work

Alexander Stolte

Expert
Licensed User
Longtime User
I need the WordWrap feature if the text is too long for the width of the label. Unfortunately, it writes the text in the area that is no longer visible. Is this a bug or am I using the BBLabel incorrectly?

B4X:
    BBLabel1.TextEngine = TextEngine
    BBLabel1.WordWrap = True
    BBLabel1.Text = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."

1735167197503.png


Example Project is in the attachment
 

Attachments

  • BBLabel WordWrap.zip
    8.9 KB · Views: 130

PaulMeuris

Well-Known Member
Licensed User
1735180109240.png

On the left with the yellow border is the BBLabel (a lightweight label as @Erel calls it in: bbcodeview and textengine )
On the right with the red border is a normal Label.
The labels have an alignment TOP_LEFT and a Wrap Text checked in the designer.
The wrap text works but the text is too long for the label.
A normal label will truncate the text and add ... at the end, the BBLabel doesn't do that.
Maybe you could have a look into the property settings of the TextEngine that is being used.

EDIT: when you use a BBCodeView a scrollbar will appear if the text is too long.
1735184986845.png


EDIT 2: the height of the BBLabel can be adjusted to the length of the text.
1735186858289.png

The code for this you can find in: BBLabel real height (uncheck handle resize event in the layout).
B4X:
    BBLabel1.Text = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
    Dim TextHeight As Int = BBLabel1.Paragraph.Height / TextEngine.mScale  + BBLabel1.Padding.Top + BBLabel1.Padding.Bottom
    BBLabel1.mBase.Height = TextHeight
Of course this can only be done if there is room to let the BBLabel expand. (i had to move the BBCodeView to the right)
 
Last edited:
Upvote 1

Alexander Stolte

Expert
Licensed User
Longtime User
Thanks for your answers, I need the truncated text, as only the height available for the label is to be used. It should not be scrolled, nor should the height of the label be adjusted.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
A possible solution for B4J:
B4X:
Private Sub UpdateBBLabelHeight(lbl As BBLabel)
    Dim par As BCParagraph = lbl.Paragraph
    If par.IsInitialized = False Then Return
    Dim MaxHeight As Int = lbl.mBase.Height
    Dim Rectangle As JavaObject
    For Each line As BCTextLine In par.TextLines
        If lbl.Padding.Top + (line.BaselineY + line.MaxHeightBelowBaseLine) / TextEngine.mScale > MaxHeight Then
            Dim Width As Double = lbl.mBase.Width
            Dim Height As Double = lbl.Padding.Top + (line.BaselineY - line.MaxHeightAboveBaseLine) / TextEngine.mScale - 1dip
            Rectangle.InitializeNewInstance("javafx.scene.shape.Rectangle", Array(Width, Height))
            Exit
        End If
    Next
    lbl.mBase.As(JavaObject).RunMethod("setClip", Array(IIf(Rectangle.IsInitialized, Rectangle, Null)))
End Sub

Private Sub B4XPage_Resize (Width As Int, Height As Int)
    UpdateBBLabelHeight(BBLabel1)
End Sub

Note that you will need to remove the BBLabel border. If a border is needed then put the label inside a panel.
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
A possible solution for B4J:
A B4X variant:
B4X:
Private Sub UpdateBBLabelHeight(lbl As BBLabel)
    Dim par As BCParagraph = lbl.Paragraph
    If par.IsInitialized = False Then Return
    Dim MaxHeight As Int = lbl.mBase.Height
    For Each line As BCTextLine In par.TextLines
        If lbl.Padding.Top + (line.BaselineY + line.MaxHeightBelowBaseLine) / m_TextEngine.mScale > MaxHeight Then
            Dim Height As Double = lbl.Padding.Top + (line.BaselineY - line.MaxHeightAboveBaseLine) / m_TextEngine.mScale - 1dip
            
            Dim ClipPanel As B4XView = xui.CreatePanel("")
            ClipPanel.Color = xui.Color_Transparent
            lbl.mBase.AddView(ClipPanel,0,Height,lbl.mBase.Width,Height)
            lbl.mBase.Top = lbl.mBase.Top + (lbl.mBase.Height - Height)
            'lbl.Text = lbl.Text & "..."

            Exit
        End If
    Next
End Sub

Test-ezgif.com-resize.gif


Just need to add an ellipsis (...) if the text is too long
 
Upvote 0
Top