Kivy Scaling Text Based On Window Height And Width
I want to scale a text inside a button or label based on the window height, but limited by window width. The following works: font_size: self.height - dp(15) However the text can
Solution 1:
You can use a scale transformation to shrink the text if it is too large, which will avoid the race:
<ScaleLabel@Label>:
_scale: 1. if self.texture_size[0] < self.width else float(self.width) / self.texture_size[0]
canvas.before:
PushMatrix
Scale:
origin: self.center
x: self._scale or1.
y: self._scale or1.
canvas.after:
PopMatrix
This does scale everything on the canvas, however. So if you're trying to draw a background or something, make sure it is outside the PushMatrix
/PopMatrix
. For example, if you wanted to use this with a Button
, you could rewrite Button
's kv rules:
<-ScaleButton@Button>:
state_image: self.background_normal if self.state == 'normal'else self.background_down
disabled_image: self.background_disabled_normal if self.state == 'normal'else self.background_disabled_down
_scale: 1.if self.texture_size[0] < self.width elsefloat(self.width) / self.texture_size[0]
canvas:
Color:
rgba: self.background_color
BorderImage:
border: self.border
pos: self.pos
size: self.size
source: self.disabled_image if self.disabled else self.state_image
PushMatrix
Scale:
origin: self.center
x: self._scale or1.
y: self._scale or1.
Color:
rgba: self.disabled_color if self.disabled else self.color
Rectangle:
texture: self.texture
size: self.texture_size
pos: int(self.center_x - self.texture_size[0] / 2.), int(self.center_y - self.texture_size[1] / 2.)
PopMatrix
I created a usage example as a gist: https://gist.github.com/kived/862db38078170ec0ef83
Post a Comment for "Kivy Scaling Text Based On Window Height And Width"