Skip to content

Commit 181929e

Browse files
authored
Merge pull request #7 from makermelissa/main
Add text anchoring and use in bitcoin example
2 parents 397f139 + aac9763 commit 181929e

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

adafruit_magtag/magtag.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def __init__(
110110
self._text_scale = []
111111
self._text_font = []
112112
self._text_line_spacing = []
113+
self._text_anchor_point = []
113114

114115
gc.collect()
115116

@@ -124,6 +125,7 @@ def add_text(
124125
text_transform=None,
125126
text_scale=1,
126127
line_spacing=1.25,
128+
text_anchor_point=(0, 0.5),
127129
):
128130
"""
129131
Add text labels with settings
@@ -153,6 +155,10 @@ def add_text(
153155
text_transform = None
154156
if not isinstance(text_scale, (int, float)) or text_scale < 1:
155157
text_scale = 1
158+
if not isinstance(text_anchor_point, (tuple, list)):
159+
text_anchor_point = (0, 0.5)
160+
if not 0 <= text_anchor_point[0] <= 1 or not 0 <= text_anchor_point[1] <= 1:
161+
raise ValueError("Text anchor point values should be between 0 and 1.")
156162
text_scale = round(text_scale)
157163
gc.collect()
158164

@@ -166,6 +172,7 @@ def add_text(
166172
self._text_transform.append(text_transform)
167173
self._text_scale.append(text_scale)
168174
self._text_line_spacing.append(line_spacing)
175+
self._text_anchor_point.append(text_anchor_point)
169176

170177
# pylint: enable=too-many-arguments
171178

@@ -255,8 +262,8 @@ def set_text(self, val, index=0, auto_refresh=True):
255262
self._text_font[index], text=string, scale=self._text_scale[index]
256263
)
257264
self._text[index].color = self._text_color[index]
258-
self._text[index].x = self._text_position[index][0]
259-
self._text[index].y = self._text_position[index][1]
265+
self._text[index].anchor_point = self._text_anchor_point[index]
266+
self._text[index].anchored_position = self._text_position[index]
260267
self._text[index].line_spacing = self._text_line_spacing[index]
261268
elif index_in_splash is not None:
262269
self._text[index] = None

examples/bitcoin_demo.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ def text_transform(val):
2424
magtag.add_text(
2525
text_font=terminalio.FONT,
2626
text_position=(
27-
10,
27+
(magtag.graphics.display.width // 2) - 1,
2828
(magtag.graphics.display.height // 2) - 1,
2929
),
3030
text_scale=3,
3131
text_transform=text_transform,
32+
text_anchor_point=(0.5, 0.5),
3233
)
3334

3435
magtag.preload_font(b"$012345789") # preload numbers

0 commit comments

Comments
 (0)