How to set Font size or Label size to fit all device
Solution 1
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 Label
class 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 size
l.texture_update()
# Set it to image, it'll be scaled to image size automatically:
self.texture = l.texture
class RootWidget(BoxLayout):
pass
class TestApp(App):
def build(self):
return MyLabel(text='Test test test')
if __name__ == '__main__':
TestApp().run()
Far from ideal, but does job.
Result:
Solution 2
To scale the font of the button or label when resizing window, I use font_size in kv file and divide self.width by some number:
Label:
size_hint: 1, 0.1
text: "Some label text"
font_size: self.width/20
eyllanesc
Developer in qt qt4 qt5 qt6 pyqt pyqt4 pyqt5 pyqt6 qml pyside pyside2 pyside6 My StackRating badge: If my answers and project help you to reduce development time then you can donate me a cup of ☕️ 😄 If some of the links I have provided in my answers have been broken please post an issue in the repository. Most of my answers can be found in https://github.com/eyllanesc/stackoverflow I have created a repo with translations of the official Qt examples from C++ to python, in addition to other examples: QtExamples If you want me to help you develop some work then you can write to my email: [email protected]. Please do not write me an email to answer any particular question. If you've ever asked a question here, please consider reading this.
Updated on July 09, 2022Comments
-
eyllanesc almost 2 years
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
. Thissize_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 theWindow.size
in cm, and usefont_size
in cm.Thanks in advance.
-
Admin over 6 yearsThanks, this is Out-of-box. I tried the same concept in Python 2 and it did not seem to work, only gives blank white. My code is in the post. In yours :
on_text
oron_texture
? -
Mikhail Gerasimov over 6 years@Arief no code is correct. I just checked and it'll work in Python 2 also - without any changes.
on_text
- is not some member of classImage
, but function that automatically called whentext
property changes. Read this link for details.