diff --git a/adafruit_magtag/magtag.py b/adafruit_magtag/magtag.py index ce97a82..aecf5b4 100755 --- a/adafruit_magtag/magtag.py +++ b/adafruit_magtag/magtag.py @@ -110,6 +110,7 @@ def __init__( self._text_scale = [] self._text_font = [] self._text_line_spacing = [] + self._text_anchor_point = [] gc.collect() @@ -124,6 +125,7 @@ def add_text( text_transform=None, text_scale=1, line_spacing=1.25, + text_anchor_point=(0, 0.5), ): """ Add text labels with settings @@ -153,6 +155,10 @@ def add_text( text_transform = None if not isinstance(text_scale, (int, float)) or text_scale < 1: text_scale = 1 + if not isinstance(text_anchor_point, (tuple, list)): + text_anchor_point = (0, 0.5) + if not 0 <= text_anchor_point[0] <= 1 or not 0 <= text_anchor_point[1] <= 1: + raise ValueError("Text anchor point values should be between 0 and 1.") text_scale = round(text_scale) gc.collect() @@ -166,6 +172,7 @@ def add_text( self._text_transform.append(text_transform) self._text_scale.append(text_scale) self._text_line_spacing.append(line_spacing) + self._text_anchor_point.append(text_anchor_point) # pylint: enable=too-many-arguments @@ -255,8 +262,8 @@ def set_text(self, val, index=0, auto_refresh=True): self._text_font[index], text=string, scale=self._text_scale[index] ) self._text[index].color = self._text_color[index] - self._text[index].x = self._text_position[index][0] - self._text[index].y = self._text_position[index][1] + self._text[index].anchor_point = self._text_anchor_point[index] + self._text[index].anchored_position = self._text_position[index] self._text[index].line_spacing = self._text_line_spacing[index] elif index_in_splash is not None: self._text[index] = None diff --git a/examples/bitcoin_demo.py b/examples/bitcoin_demo.py index 6047029..4518d49 100644 --- a/examples/bitcoin_demo.py +++ b/examples/bitcoin_demo.py @@ -24,11 +24,12 @@ def text_transform(val): magtag.add_text( text_font=terminalio.FONT, text_position=( - 10, + (magtag.graphics.display.width // 2) - 1, (magtag.graphics.display.height // 2) - 1, ), text_scale=3, text_transform=text_transform, + text_anchor_point=(0.5, 0.5), ) magtag.preload_font(b"$012345789") # preload numbers