Label (
aligment,
text,
size,
显示 (
Label.shorten,
Label.max_lines),
自定义文字样式
api-kivy.core.text.markup,
font_size='20sp',
宽度指定,高度自适应
Label:
text_size: root.width, None
size: self.texture_size,
Usage example
The following example marks the anchors and references contained in a label::from kivy.app import App
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.graphics import Color, Rectangle
class TestApp(App):
@staticmethod
def get_x(label, ref_x):
""" Return the x value of the ref/anchor relative to the canvas """
return label.center_x - label.texture_size[0] * 0.5 + ref_x
@staticmethod
def get_y(label, ref_y):
""" Return the y value of the ref/anchor relative to the canvas """
# Note the inversion of direction, as y values start at the top of
# the texture and increase downwards
return label.center_y + label.texture_size[1] * 0.5 - ref_y
def show_marks(self, label):
# Indicate the position of the anchors with a red top marker
for name, anc in label.anchors.items():
with label.canvas:
Color(1, 0, 0)
Rectangle(pos=(self.get_x(label, anc[0]),
self.get_y(label, anc[1])),
size=(3, 3))
# Draw a green surround around the refs. Note the sizes y inversion
for name, boxes in label.refs.items():
for box in boxes:
with label.canvas:
Color(0, 1, 0, 0.25)
Rectangle(pos=(self.get_x(label, box[0]),
self.get_y(label, box[1])),
size=(box[2] - box[0],
box[1] - box[3]))
def build(self):
label = Label(
text='[anchor=a]a\\nChars [anchor=b]b\\n[ref=myref]ref[/ref]',
markup=True)
Clock.schedule_once(lambda dt: self.show_marks(label), 1)
return label
TestApp().run()
,
def __init__(
self, text='',
font_size=12,
font_name=DEFAULT_FONT,
bold=False,
italic=False,
halign='left',
valign='bottom',
shorten=False,
text_size=None,
mipmap=False,
color=None,
line_height=1.0,
strip=False,
strip_reflow=True,
shorten_from='center',
split_str=' ',
unicode_errors='replace',
**kwargs
):
)