Skip to content

Commit 9a73454

Browse files
author
Margaret Matocha
committed
Added getter/setter for font to ensure proper screen update when font is changed
1 parent f26a881 commit 9a73454

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

adafruit_display_text/label.py

100644100755
Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,18 @@
2222
"""
2323
`adafruit_display_text.label`
2424
====================================================
25-
2625
Displays text labels using CircuitPython's displayio.
27-
2826
* Author(s): Scott Shawcroft
29-
3027
Implementation Notes
3128
--------------------
32-
3329
**Hardware:**
34-
3530
**Software and Dependencies:**
36-
3731
* Adafruit CircuitPython firmware for the supported boards:
3832
https://github.com/adafruit/circuitpython/releases
39-
4033
"""
4134

35+
print('loading label.py Yay!')
36+
4237
import displayio
4338

4439
__version__ = "0.0.0-auto.0"
@@ -50,7 +45,6 @@ class Label(displayio.Group):
5045
properties will be the left edge of the bounding box, and in the center of a M
5146
glyph (if its one line), or the (number of lines * linespacing + M)/2. That is,
5247
it will try to have it be center-left as close as possible.
53-
5448
:param Font font: A font class that has ``get_bounding_box`` and ``get_glyph``.
5549
Must include a capital M for measuring character size.
5650
:param str text: Text to display
@@ -77,7 +71,7 @@ def __init__(
7771
max_glyphs = len(text)
7872
super().__init__(max_size=max_glyphs, **kwargs)
7973
self.width = max_glyphs
80-
self.font = font
74+
self._font = font
8175
self._text = None
8276
self._anchor_point = (0, 0)
8377
self.x = x
@@ -94,7 +88,7 @@ def __init__(
9488
self._transparent_background = True
9589
self.palette[1] = color
9690

97-
bounds = self.font.get_bounding_box()
91+
bounds = self._font.get_bounding_box()
9892
self.height = bounds[1]
9993
self._line_spacing = line_spacing
10094
self._boundingbox = None
@@ -107,9 +101,11 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
107101
y = 0
108102
i = 0
109103
old_c = 0
104+
bounds = self._font.get_bounding_box() # moved here ***
105+
self.height = bounds[1] # moved here ***
110106
y_offset = int(
111107
(
112-
self.font.get_glyph(ord("M")).height
108+
self._font.get_glyph(ord("M")).height
113109
- new_text.count("\n") * self.height * self.line_spacing
114110
)
115111
/ 2
@@ -121,7 +117,7 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
121117
y += int(self.height * self._line_spacing)
122118
x = 0
123119
continue
124-
glyph = self.font.get_glyph(ord(character))
120+
glyph = self._font.get_glyph(ord(character))
125121
if not glyph:
126122
continue
127123
right = max(right, x + glyph.width)
@@ -176,7 +172,7 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
176172
and old_c < len(self._text)
177173
and (
178174
self._text[old_c] == "\n"
179-
or not self.font.get_glyph(ord(self._text[old_c]))
175+
or not self._font.get_glyph(ord(self._text[old_c]))
180176
)
181177
):
182178
old_c += 1
@@ -238,6 +234,17 @@ def text(self):
238234
def text(self, new_text):
239235
self._update_text(str(new_text))
240236

237+
@property
238+
def font(self):
239+
return self._font
240+
241+
@font.setter
242+
def font(self, newFont):
243+
old_text=self._text
244+
self._text=''
245+
self._font=newFont
246+
self._update_text(str(old_text))
247+
241248
@property
242249
def anchor_point(self):
243250
"""Point that anchored_position moves relative to.

0 commit comments

Comments
 (0)