I have a label with text that needs its
to fit in some rectangle. I think I should get size in pixels like this (formulas):
# Device independent pixels px = dp * density # Scale independent pixels px = sp * density * fontscale # Points px = pt * dpi / 72.
But problem is that I can’t get dpi, density, fontscale
, etc., it says:
TypeError: 'float' object is not callable
Also it seems there is no built-in function that can fit the text.
I think, I’ve found the solution.
class BlockLabel(Label): scale_factor = .9 factor = dimension = None def on_texture_size(self, *args): if not self.factor: self.factor = [self.font_size / self.texture_size, self.font_size / self.texture_size] if not self.dimension: self.dimension = 1 if self.texture_size * self.size < self.texture_size * self.size else 0 self.font_size = self.size[self.dimension] * self.scale_factor * self.factor[self.dimension]
I’ve tested it only on squares (I mean text inscribed in square) for now, but it should work on rectangles too. Also if there are only
can be removed like this:
self.dimension = 1 if self.texture_size < self.texture_size else 0
And I’ve tested this code only for one-line text, I don’t know if it works for multi-line text correctly, although it should.