Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow adaptive width for UILayout with multiline text #1946

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading