text.size()
method does not change the height
attribute of the label. If the text is too long, it will overlap with the content below:
from kivy.app import App from kivy.uix.label import Label from kivy.uix.button import Button from kivy.uix.gridlayout import GridLayout class DynamicHeight(App): def build(self): layout = GridLayout(cols=1, spacing=20) l = Label(text='! '*100, text_size=(10, None), size_hint_y=None, height=10) b = Button(text='...', size_hint_y=None) layout.add_widget(l) layout.add_widget(b) return layout DynamicHeight().run()
You need to calculate the height of the text and set it manually with the height
attribute. I do not know anything good and clean way to do this. Here is the dirty way:
before = label._label.render() label.text_size=(300, None) after = label._label.render() label.height = (after[1]/before[1])*before[1]
Example:
from kivy.app import App from kivy.uix.label import Label from kivy.uix.gridlayout import GridLayout from kivy.uix.scrollview import ScrollView class DynamicHeight(App): def build(self): layout = GridLayout(cols=1, size_hint_y=None, spacing=20) layout.bind(minimum_height=layout.setter('height')) for i in xrange(1, 20): l = Label(text='Text ' * (i*10), text_size=(300, None), size_hint_y=None)
source share