背景矩形适合 Kivy 中的标签文本
Background rectangle fit to label text in Kivy
我正在尝试在标签周围设置一个背景,该背景适合其中文本中的行数。例如,带有文本 'Line1\nLine1\nLine3' 的标签将具有更大的 Y 维度,仅 'line1'
目前发生的情况是所有的标签都是相同的大小和不适合它们的剪裁文本,标签也在回收视图布局内,因为我希望能够滚动和更新大标签数量经常。
我已经尝试了一些方法,但都没有成功,并且正在努力让一个变量被理解,我在 .kv 文件中添加了 # HERE
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'
感谢您的帮助:)
编辑:
我发现我一直在更改错误的值,并且需要更改的变量已用#NewHERE 标记,但是我仍然无法让它工作或从 py 文件中获取变量到 kv
为了获得随着 text-content 增长而垂直扩展的 Label
,您可以将其高度设置为其纹理高度。
此外,要使文本适合可用 space(宽度),您可以更改 text_size
。
因此您可以将 LabelColor
的 kvlang 修改为,
<LabelColor>:
color: 0,0,0,1
text_size: self.width, None
size_hint_y: None
height: self.texture_size[1]
我正在尝试在标签周围设置一个背景,该背景适合其中文本中的行数。例如,带有文本 'Line1\nLine1\nLine3' 的标签将具有更大的 Y 维度,仅 'line1'
目前发生的情况是所有的标签都是相同的大小和不适合它们的剪裁文本,标签也在回收视图布局内,因为我希望能够滚动和更新大标签数量经常。
我已经尝试了一些方法,但都没有成功,并且正在努力让一个变量被理解,我在 .kv 文件中添加了 # HERE
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'
感谢您的帮助:)
编辑: 我发现我一直在更改错误的值,并且需要更改的变量已用#NewHERE 标记,但是我仍然无法让它工作或从 py 文件中获取变量到 kv
为了获得随着 text-content 增长而垂直扩展的 Label
,您可以将其高度设置为其纹理高度。
此外,要使文本适合可用 space(宽度),您可以更改 text_size
。
因此您可以将 LabelColor
的 kvlang 修改为,
<LabelColor>:
color: 0,0,0,1
text_size: self.width, None
size_hint_y: None
height: self.texture_size[1]