Skip to content

Commit

Permalink
Allow adaptive width for UILayout with multiline text (#1946)
Browse files Browse the repository at this point in the history
  • Loading branch information
eruvanos authored Jan 11, 2024
1 parent 22b8b6e commit 5f888d5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
15 changes: 13 additions & 2 deletions arcade/gui/widgets/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ def __init__(
size_hint_max=None,
**kwargs,
):
# If multiline is enabled and no width is given, we need to fit the
# size to the text. This is done by setting the width to a very
# large value and then fitting the size.
adaptive_multiline = False
if multiline and not width:
width = 999999
adaptive_multiline = True

# Use Arcade Text wrapper of pyglet.Label for text rendering
self.label = arcade.Text(
start_x=0,
Expand All @@ -99,10 +107,13 @@ def __init__(
bold=bold,
italic=italic,
align=align,
anchor_y="bottom", # Position text bottom left to fit into scissor
multiline=multiline, # area
anchor_y="bottom", # Position text bottom left to fit into scissor area
multiline=multiline,
**kwargs,
)
if adaptive_multiline:
# +1 is required to prevent line wrap
width = self.label.content_width + 1

super().__init__(
x=x,
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/gui/test_uilabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,12 @@ def test_uilabel_fixes_internal_text_to_pos_0_0(window):
assert label.label.position == (0, 0)
assert label.position == (10, 10)

def test_adaptive_width_support_for_multiline_text(window):
"""
This test is a bit tricky. Enabling multiline without a width
should fit the size to the text. This is not natively supported by either arcade.Text or pyglet.Label.
Because text length variates between different os, we can only test boundaries, which indicate a proper implementation.
"""
label = UILabel(text="Multiline\ntext\nwhich\n", multiline=True)
assert label.width < 100
assert label.height > 20

0 comments on commit 5f888d5

Please sign in to comment.