How to set kivy font size or label size so that it will fit all phone-device screen? (fit by means does not overlap with the boundary, rectangle, or even the screen)
I know the Buttons and some other Widgets in Kivy have size_hint
. This size_hint
does not give desired result.
Also, setting the font_size
can do the trick, but it will be difficult to automate using this. Now close solution is by setting and getting the Window.size
in cm, and use font_size
in cm.
Thanks in advance.
This is actually interesting question.
Usually you pass text, font, font size and region that limits the text (text_size
in kivy label) to renderer and get texture of some size from it. What you want is knowing label's texture size to get font size. I guess it's theoretically possible, but you'll need to do all calculations manually in Kivy.
May us do some workaround? Instead of trying to calculate font size, we can just use some very large one and scale final texture to match given size. Scaling with ration keeping can be easily done with Kivy's Image
widget, for example. Proof-of-Concept (Python 3):
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.uix.label import Labelclass MyLabel(Image):text = StringProperty('')def on_text(self, *_):# Just get large texture:l = Label(text=self.text)l.font_size = '1000dp' # something that'll give texture bigger than phone's screen sizel.texture_update()# Set it to image, it'll be scaled to image size automatically:self.texture = l.textureclass RootWidget(BoxLayout):passclass TestApp(App):def build(self):return MyLabel(text='Test test test')if __name__ == '__main__':TestApp().run()
Far from ideal, but does job.
Result: