Skip to content

Commit 5f888d5

Browse files
authored
Allow adaptive width for UILayout with multiline text (#1946)
1 parent 22b8b6e commit 5f888d5

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

arcade/gui/widgets/text.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ def __init__(
8787
size_hint_max=None,
8888
**kwargs,
8989
):
90+
# If multiline is enabled and no width is given, we need to fit the
91+
# size to the text. This is done by setting the width to a very
92+
# large value and then fitting the size.
93+
adaptive_multiline = False
94+
if multiline and not width:
95+
width = 999999
96+
adaptive_multiline = True
97+
9098
# Use Arcade Text wrapper of pyglet.Label for text rendering
9199
self.label = arcade.Text(
92100
start_x=0,
@@ -99,10 +107,13 @@ def __init__(
99107
bold=bold,
100108
italic=italic,
101109
align=align,
102-
anchor_y="bottom", # Position text bottom left to fit into scissor
103-
multiline=multiline, # area
110+
anchor_y="bottom", # Position text bottom left to fit into scissor area
111+
multiline=multiline,
104112
**kwargs,
105113
)
114+
if adaptive_multiline:
115+
# +1 is required to prevent line wrap
116+
width = self.label.content_width + 1
106117

107118
super().__init__(
108119
x=x,

tests/unit/gui/test_uilabel.py

+9
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,12 @@ def test_uilabel_fixes_internal_text_to_pos_0_0(window):
5656
assert label.label.position == (0, 0)
5757
assert label.position == (10, 10)
5858

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

0 commit comments

Comments
 (0)