How can I use the text in the label in Kivy?

微信扫一扫,分享到朋友圈

How can I use the text in the label in Kivy?

I have a label with text that needs its font_size
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[0], self.font_size / self.texture_size[1]]
        if not self.dimension:
            self.dimension = 1 if self.texture_size[0] * self.size[1] < self.texture_size[1] * self.size[0] 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
squares, self.size
can be removed like this:

self.dimension = 1 if self.texture_size[0] < self.texture_size[1] 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.

微信扫一扫,分享到朋友圈

How can I use the text in the label in Kivy&quest;

FBI forces suspect to unlock his Apple iPhone X using Face ID

上一篇

一文看懂家装发展史:跑路关店浪潮之下,家装业的春风吹往何处?

下一篇

你也可能喜欢

How can I use the text in the label in Kivy&quest;

长按储存图像,分享给朋友