Will Jordan
Will Jordan

Reputation: 246

Background rectangle fit to label text in Kivy

I am trying to get a background around a label that fits the number of lines in the texted in it. For example, a label with text 'Line1\nLine1\nLine3' would have a larger Y dimension that just 'line1'

What currently happens is all of the labels are the same size and clip-off text that doesn't fit within them, the labels are also inside a recycleview layout because I would like to be able to scroll and update large amount of the labels often.

I have tried a few things but have had no luck, and am struggling to get a variable to be understood where I have added # HERE in the .kv file

from kivy.app import App
from kivy.properties import NumericProperty, Clock, ObjectProperty, StringProperty
from kivy.uix.widget import Widget
from kivy.uix.recycleview import RecycleView
from kivy.uix.button import Button
from kivy.uix.label import Label


class TopPostsTest(RecycleView):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        message_height = NumericProperty(20)
        items = ["hello\ntest\ntest", "test, gghgjhgjhgjhgjhghjghjgjhgjhgjhgjhgjhgjhgjhgjhg", "cheese"]
        self.data = [{'text':str(p)} for p in items]

class LabelColor(Label):
    pass

class TruthApp(App):
#    def build(self):
#        return super().build()
    pass

if __name__ == "__main__":
    TruthApp().run()
<MainControl@PageLayout>:
    border: "25dp"
    swipe_threshold: 0.4
    TopPostsTest:
    Settings:

<LabelColor>:
    color: 0,0,0,1
    text_size: self.size
    halign: 'left'
    valign: 'top'
    font_name: "Assets/Fonts/Nunito-Bold.ttf"
    font_size: "12dp"
    multiline: True

    canvas.before:
        Color:
            rgba: (0.8, 0.8, 0.8, 1)
        RoundedRectangle:
            pos: self.pos
            size: self.size
            radius: [5, 5, 5, 5]
    canvas:
        Color:
            rgba:0,0.9,0.9,1
        Line:
            width:0.8
            rounded_rectangle:(self.x,self.y,self.width,root.height, 5) # HERE

<TopPostsTest>:
    viewclass: 'LabelColor'
    scroll_y: 1
    RecycleBoxLayout:
        id: message_view
        default_size: None, dp(40) # NewHERE
        default_size_hint: 1, None
        size_hint_y: None
        padding: ["10dp", "16dp"]
        spacing: "8dp"
        height: self.minimum_height
        orientation: 'vertical'

Thank you for any help :)

Edit: I have found that I have been changing the wrong value and that the variable that needs changing has been marker with # NewHERE, however I am still unable to get it to work or get a variable from the py file into the kv

Upvotes: 0

Views: 428

Answers (1)

ApuCoder
ApuCoder

Reputation: 2888

In order to get a Label that expands vertically as it's text-content grows you can set its height to its texture height.

Also, to fit the text within available space (width) you can change text_size.

Thus you can modify the kvlang for LabelColor as,

<LabelColor>:
    color: 0,0,0,1
    text_size: self.width, None
    size_hint_y: None
    height: self.texture_size[1]

Upvotes: 1

Related Questions